删除目录中文件的最快方法是什么? (特定文件扩展名除外)

发布于 2024-10-17 04:13:28 字数 261 浏览 2 评论 0原文

我见过类似的问题 清空目录的最佳方法是什么?

但我需要知道,

删除目录中找到的所有文件(找到的任何 .zip 文件除外)的最快方法是什么。

这里闻起来像 linq...还是什么?

我所说的最快方式是指最快的执行时间。

I have seen questions like What is the best way to empty a directory?

But I need to know,

what is the fastest way of deleting all the files found within the directory, except any .zip files found.

Smells like linq here... or what?

By saying fastest way, I mean the Fastest execution time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

御弟哥哥 2024-10-24 04:13:28

如果您使用 .NET 4,您可以受益于 .NET 现在并行化您的功能的智能方式。此代码是执行此操作的快速方法。这也随着处理器上的核心数量而变化。

DirectoryInfo di = new DirectoryInfo(yourDir);
var files = di.GetFiles();

files.AsParallel().Where(f => f.Extension != ".zip").ForAll((f) => f.Delete());

If you are using .NET 4 you can benifit the smart way .NET now parallizing your functions. This code is the fasted way to do it. This scales with your numbers of cores on the processor too.

DirectoryInfo di = new DirectoryInfo(yourDir);
var files = di.GetFiles();

files.AsParallel().Where(f => f.Extension != ".zip").ForAll((f) => f.Delete());
爱你不解释 2024-10-24 04:13:28

所谓最快,是指最少的代码行数还是最快的执行时间?下面是一个使用 LINQ 与每个循环并行来快速删除它们的示例。

string[] files = System.IO.Directory.GetFiles("c:\\temp", "*.*", IO.SearchOption.TopDirectoryOnly);

List<string> del = (
   from string s in files
   where ! (s.EndsWith(".zip"))
   select s).ToList();

Parallel.ForEach(del, (string s) => { IO.File.Delete(s); });

By fastest are you asking for the least lines of code or the quickest execution time? Here is a sample using LINQ with a parallel for each loop to delete them quickly.

string[] files = System.IO.Directory.GetFiles("c:\\temp", "*.*", IO.SearchOption.TopDirectoryOnly);

List<string> del = (
   from string s in files
   where ! (s.EndsWith(".zip"))
   select s).ToList();

Parallel.ForEach(del, (string s) => { IO.File.Delete(s); });
近箐 2024-10-24 04:13:28

在撰写此答案时,之前的答案都没有使用 Directory.EnumerateFiles() ,它允许您在构建列表时对文件列表进行操作。
代码:

Parallel.ForEach(Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories).AsParallel(), Item =>
        {
            if(!string.Equals(Path.GetExtension(Item), ".zip",StringComparison.OrdinalIgnoreCase))
                File.Delete(Item);
        });

据我所知,在这种情况下使用 AsParallel() 的性能增益应该不显着(如果找到),但它确实对我的情况产生了影响。

我比较了使用 1-foreach 删除 4689 个文件列表中除 .zip 文件之外的所有文件所需的时间,其中 10 个是 zip 文件。 2-并行foreach。 3-IEnumerable().AsParallel().ForAll。使用 IEnumerable().AsParallel() 的 4 并行 foreach,如上所示。
结果:

1-1545

2-1015

3-1103

4-839

第五种也是最后一种情况是使用 Directory.GetFiles()

5-2266 的

正常 foreach ,当然,据我所知,结果并不是决定性的正确的基准测试您需要使用 RAM 驱动器而不是 HDD 。

注意:随着文件数量的增加,EnumerateFiles 和 GetFiles 之间的性能差异变得更加明显。

At the time of writing this answer none of the previous answers used Directory.EnumerateFiles() which allows you to carry on operations on the list of files while the list is being constructed .
Code:

Parallel.ForEach(Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories).AsParallel(), Item =>
        {
            if(!string.Equals(Path.GetExtension(Item), ".zip",StringComparison.OrdinalIgnoreCase))
                File.Delete(Item);
        });

as far as I know the performance gain from using AsParallel() shouldn't be significant(if found) in this case however it did make difference in my case.

I compared the time it takes to delete all but .zip files in a list of 4689 files of which 10 were zip files using 1-foreach. 2-parallel foreach. 3-IEnumerable().AsParallel().ForAll. 4-parallel foreach using IEnumerable().AsParallel() as illustrated above.
Results:

1-1545

2-1015

3-1103

4-839

the fifth and the last case was a normal foreach using Directory.GetFiles()

5-2266

of course the results weren't conclusive , as far as I know to carry on a proper benchmarking you need to use a ram drive instead of a HDD .

Note:that the performance difference between EnumerateFiles and GetFiles becomes more apparent as the number of files increases.

罗罗贝儿 2024-10-24 04:13:28

这是普通的旧式 C#

foreach(string file in Directory.GetFiles(Server.MapPath("~/yourdirectory")))
{
    if(Path.GetExtension(file) != ".zip")
    {
        File.Delete(file);
    }
}

这是 LINQ

var files = from f in Directory.GetFiles("")
            where Path.GetExtension(f) != ".zip"
            select f;

foreach(string file in files)
    File.Delete(file);

Here's plain old C#

foreach(string file in Directory.GetFiles(Server.MapPath("~/yourdirectory")))
{
    if(Path.GetExtension(file) != ".zip")
    {
        File.Delete(file);
    }
}

And here's LINQ

var files = from f in Directory.GetFiles("")
            where Path.GetExtension(f) != ".zip"
            select f;

foreach(string file in files)
    File.Delete(file);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文