What is better way to delete file with condition

2019-09-15 15:10发布

I want to build a win service(no UI) on c# that all what it done is: run on list of directories and delete files that over then X kb. I want the better performance,

what is the better way to do this? there is no pure async function for delete file so if i want to use async await I can wrap this function like:

public static class FileExtensions {
   public static Task DeleteAsync(this FileInfo fi) {
      return Task.Factory.StartNew(() => fi.Delete() );
   }
}

and call to this function like:

FileInfo fi = new FileInfo(fileName);
await fi.DeleteAsync();

i think to run like

foreach file on ListOfDirectories
{

  if(file.Length>1000)
      await file.DeleteAsync

}

but on this option the files will delete 1 by 1 (and every DeleteAsync will use on thread from the threadPool). so i not earn from the async, i can do it 1 by 1.

maybe i think to collect X files on list and then delete them AsParallel

please help me to find the better way

2条回答
成全新的幸福
2楼-- · 2019-09-15 15:28

You can use Directory.GetFiles("DirectoryPath").Where(x=> new FileInfo(x).Length < 1000); to get a list of files that are under 1 KB of size.

Then use Parallel.ForEach to iterate over that collection like this:

var collectionOfFiles = Directory.GetFiles("DirectoryPath")
                                 .Where(x=> new FileInfo(x).Length < 1000);

Parallel.ForEach(collectionOfFiles, File.Delete);

It could be argued that you should use:

Parallel.ForEach(collectionOfFiles, currentFile =>
{
    File.Delete(currentFile);
});

to improve the readability of the code.

MSDN has a simple example on how to use Parallel.ForEach()

If you are wondering about the FileInfo object, here is the documentation

查看更多
欢心
3楼-- · 2019-09-15 15:37

this is may be can help you.

public static class FileExtensions
{
    public static Task<int> DeleteAsync(this IEnumerable<FileInfo> files)
    {
        var count = files.Count();
        Parallel.ForEach(files, (f) =>
        {
            f.Delete();
        });
        return Task.FromResult(count);
    }
    public static async Task<int> DeleteAsync(this DirectoryInfo directory, Func<FileInfo, bool> predicate)
    {
        return await directory.EnumerateFiles().Where(predicate).DeleteAsync();
    }
    public static async Task<int> DeleteAsync(this IEnumerable<FileInfo> files, Func<FileInfo, bool> predicate)
    {
        return await files.Where(predicate).DeleteAsync();
    }

}

enter image description here

enter image description here

        var _byte = 1;
        var _kb = _byte * 1000;
        var _mb = _kb * 1000;
        var _gb = _mb * 1000;
        DirectoryInfo d = new DirectoryInfo(@"C:\testDirectory");

        var deletedFileCount = await d.DeleteAsync(f => f.Length > _mb * 1);
        Debug.WriteLine("{0} Files larger than 1 megabyte deleted", deletedFileCount);
        // => 7 Files larger than 1 megabyte deleted

        deletedFileCount = await d.GetFiles("*.*",SearchOption.AllDirectories)
            .Where(f => f.Length > _kb * 10).DeleteAsync();

        Debug.WriteLine("{0} Files larger than 10 kilobytes deleted", deletedFileCount);
        // => 11 Files larger than 10 kilobytes deleted
查看更多
登录 后发表回答