<分区>
Possible Duplicate:
DirectoryInfo.Delete vs Directory.Delete
我编写这段代码是为了清空一些我经常删除的文件,例如 Windows 中的临时文件和 MSN 缓存文件。
代码1
我可以添加新路径以简单的方式应用 DeleteContains 方法我只需要将路径添加到列表中
代码2
不允许我添加我想要的路径,我必须为每个路径创建新的字符串数组和一个新的循环
无论如何要使用 code1 删除文件夹的包含而不是文件夹及其包含??
在 code1 中的 foreach 中有任何变通作为 code2 的工作吗??
因为某些文件夹可以删除或删除它们会导致某些应用程序出现问题并且应用程序将无法再次运行
"C:\Users\user\AppData\Local\Temp"
而其他文件夹可以像MSN cashes一样无问题的删除
“C:\Users\user\AppData\Local\Microsoft\Windows Live\联系人”
代码1
private void Clear_Click(object sender, EventArgs e)
{
List<DirectoryInfo> FolderToClear = new List<DirectoryInfo>();
// here is a list of files I want to delete
FolderToClear.Add(new DirectoryInfo("path1"));
FolderToClear.Add(new DirectoryInfo("path2"));
FolderToClear.Add(new DirectoryInfo("path3"));
FolderToClear.Add(new DirectoryInfo("path4"));
foreach (DirectoryInfo directory in FolderToClear)
{
directory.Delete(true);
}
}
代码2
private void DeleteContents(string Path)
{
string[] DirectoryList = Directory.GetDirectories(Path1);
string[] FileList = Directory.GetFiles(Path1);
foreach (string xin FileList)
{
File.Delete(x);
}
foreach ( string x in DirectoryList)
{
Directory.Delete(x, true);
}
}