简体   繁体   English

删除数百万个文件的最快方法

[英]Fastest way to delete million of files

I have a list of string which are relative paths. 我有一个字符串列表,它们是相对路径。 I also have a string which contains root path for those files. 我也有一个字符串,其中包含这些文件的根路径。 Now I am deleting them like this: 现在,我要像这样删除它们:

foreach (var rawDocumentPath in documents.Select(x => x.RawDocumentPath))
{
    if (string.IsNullOrEmpty(rawDocumentPath))
    {
        continue;
    }
    string fileName = Path.Combine(storagePath, rawDocumentPath);
    File.Delete(fileName);
}

the problem is that I call Path.Combine for every file, and it's slow enough. 问题是我为每个文件都调用Path.Combine ,它足够慢。 How can I speed up this code? 如何加快此代码的速度? I can't delete whole folders, I cannot change current directory (because it affects a whole program)... 我无法删除整个文件夹,也无法更改当前目录(因为它会影响整个程序)...

I need something like a class which can delete fast several files in specified directory. 我需要一个可以在指定目录中快速删除多个文件的类。

If your disk can handle it, parallizing should help a lot: 如果您的磁盘可以处理它,则并行化将大有帮助:

documents.AsParallel().ForAll(
    document =>
    {
        if (!string.IsNullOrEmpty(document.RawDocumentPath))
        {
            string fileName = Path.Combine(storagePath, document.RawDocumentPath);
            File.Delete(fileName);
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM