How can I scan an entire drive and delete all files (read-only or not) with multiple specific file extension?
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!
Walk the directory tree. The requisite code is described here.
foreach (string filename in Directory.EnumerateFiles(@"C:\", "*.xxx", SearchOption.AllDirectories)
{
File.Delete(filename);
}
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);
}
}
foreach (String file in Directory.GetFiles("c:\\","*.iddqd", SearchOption.AllDirectories) )
File.Delete (file);
try this:
DirectoryInfo directoryInfo = new DirectoryInfo(@"directory path");
foreach (var f in directoryInfo.GetFiles("*.*", SearchOption.AllDirectories))
{
f.Delete();
}
精彩评论