以下是我尝试删除文件夹时的内容:
namespace sortfolder
{
class Program
{
static string path = "C:\\Work\\6.70_Extensions\\NightlyBuild\\";
static void Main(string[] args)
{
var di = new DirectoryInfo("C:\\Work\\6.70_Extensions\\NightlyBuild");
foreach (var file in di.GetFiles("*", SearchOption.AllDirectories))
file.Attributes &= ~FileAttributes.ReadOnly;
var files = Directory.GetDirectories(path, "SASE Lab Tools.*");
foreach(var file in files)
Console.WriteLine(file);
foreach(var file in files.OrderByDescending(x=>x).Skip(7))
Console.WriteLine(file);
foreach(var file in files.OrderByDescending(x=>x).Skip(7))
Directory.Delete(file);
}
}
}
如标题中所述,我想删除此文件夹及其所有内容。我该怎么做呢?显然我有IOException @ Directory.Delete(file)
因为它包含文件
答案 0 :(得分:3)
Directory.Delete
有第二个参数,它接受bool =递归。请改用重载。
答案 1 :(得分:3)
您遗漏了true
Directory.Delete(file,true);
例如
public static void Main()
{
string path = @"c:\MyDir\temp";
try
{
Directory.Delete(path, true);
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
finally {}
}