[英]Fastest way to delete million of files
我有一个字符串列表,它们是相对路径。 我也有一个字符串,其中包含这些文件的根路径。 现在,我要像这样删除它们:
foreach (var rawDocumentPath in documents.Select(x => x.RawDocumentPath))
{
if (string.IsNullOrEmpty(rawDocumentPath))
{
continue;
}
string fileName = Path.Combine(storagePath, rawDocumentPath);
File.Delete(fileName);
}
问题是我为每个文件都调用Path.Combine
,它足够慢。 如何加快此代码的速度? 我无法删除整个文件夹,也无法更改当前目录(因为它会影响整个程序)...
我需要一个可以在指定目录中快速删除多个文件的类。
如果您的磁盘可以处理它,则并行化将大有帮助:
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.