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

2023-09-03 07:35:59 作者:呆萌萝莉甜甜酱

我已经见过像什么是最好的方式问题清空目录?

不过,我需要知道,

什么是删除所有目录中找到的文件,但发现任何的.zip 文件的最快方式。

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

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

Smells like linq here... or what?

按说最快的方式,我的意思是最快的执行时间。

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

推荐答案

通过最快的是你要求为code中的至少线或最快的执行时间?下面是使用LINQ与并行每个循环快速删除它们的样本。

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); });