Sub TestIO()
Dim fld As New DirectoryInfo("C:\Logs")
fld.EnumerateFiles().
Where(Function(f) (Date.Now - f.CreationTimeUtc) > TimeSpan.FromDays(10)).
ToList().
ForEach(Sub(f) f.Delete())
End Sub
更新
摆脱
ToList
打电话和汉迪一起工作
ForEach
方法,您可以添加将直接与IEnumerable一起工作的扩展方法(这在.NET框架中是受欢迎的)。此扩展方法如下所示:
public static class ExtensionMethods
{
public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> action)
{
if (enumerable == null)
{
throw new ArgumentException($"Argument {nameof(enumerable)} is null.");
}
using (var enumerator = enumerable.GetEnumerator())
{
while (enumerator.MoveNext())
{
action(enumerator.Current);
}
}
}
}