如何扫描整个驱动器并删除具有多个特定文件扩展名的所有文件(只读或非只读)?

发布于 2024-09-14 04:39:48 字数 141 浏览 4 评论 0原文

我正在为我的应用程序开发一项功能,单击按钮即可扫描“C:\”驱动器(以及所有子目录,只读或非只读),并删除具有特定文件扩展名的所有文件。我该怎么做呢?我确信会使用列表或数组......但这就是我所知道的。

请仅限 .Net Framework 2.0!

I'm working a feature for a application of mine that on button click it scans the "C:\" drive (and all sub directory's, read-only or not), and deletes all files with specific file extensions. How would I go about doing this? I'm sure a list or a array would be used... but that's about all I know.

Please .Net framework 2.0 ONLY!

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

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

发布评论

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

评论(5

生生漫 2024-09-21 04:39:48

遍历目录树。 此处描述了必需的代码。

Walk the directory tree. The requisite code is described here.

友谊不毕业 2024-09-21 04:39:48
foreach (string filename in Directory.EnumerateFiles(@"C:\", "*.xxx", SearchOption.AllDirectories)
{
    File.Delete(filename);
}
foreach (string filename in Directory.EnumerateFiles(@"C:\", "*.xxx", SearchOption.AllDirectories)
{
    File.Delete(filename);
}
苏佲洛 2024-09-21 04:39:48

我认为下面的代码可以工作:

using System.IO;

...


string[] extensions = { "*.apa", "*.dip", "*.ep" }; // whatever extensions you care about
foreach (string ext in extensions)
{
     foreach (string file in Directory.GetFiles(@"c:\", ext, SearchOption.AllDirectories))
     {
          File.SetAttributes(file, FileAttributes.Normal);
          File.Delete(file);
     }
}

I think the following code would work:

using System.IO;

...


string[] extensions = { "*.apa", "*.dip", "*.ep" }; // whatever extensions you care about
foreach (string ext in extensions)
{
     foreach (string file in Directory.GetFiles(@"c:\", ext, SearchOption.AllDirectories))
     {
          File.SetAttributes(file, FileAttributes.Normal);
          File.Delete(file);
     }
}
墨离汐 2024-09-21 04:39:48
foreach (String file in Directory.GetFiles("c:\\","*.iddqd", SearchOption.AllDirectories) )
        File.Delete (file);
foreach (String file in Directory.GetFiles("c:\\","*.iddqd", SearchOption.AllDirectories) )
        File.Delete (file);
北方的韩爷 2024-09-21 04:39:48

试试这个:

 DirectoryInfo directoryInfo = new DirectoryInfo(@"directory path");
        foreach (var f in directoryInfo.GetFiles("*.*", SearchOption.AllDirectories))
        {
            f.Delete();
        }

try this:

 DirectoryInfo directoryInfo = new DirectoryInfo(@"directory path");
        foreach (var f in directoryInfo.GetFiles("*.*", SearchOption.AllDirectories))
        {
            f.Delete();
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文