[英]Delete all folders inside folder?
Hey guys so I'm working on a small program which sorta speeds up your pc, but I have a problem I get an exception if I try to delete files, I believe cause they are in use. 嗨,大家好,我正在开发一个小程序,可以加快您的PC速度,但是我有一个问题,如果尝试删除文件,我会遇到异常,我相信是因为它们正在使用中。 Though it deletes some, but not much. 尽管它删除了一些,但数量不多。 My question is how to delete files in use, and how to delete sub folders inside the folder 我的问题是如何删除正在使用的文件,以及如何删除文件夹内的子文件夹
//this is my directory:
DirectoryInfo tempPath = new DirectoryInfo(@"C:\Users\" + Environment.UserName + @"\AppData\Local\Temp");
private void button8_Click(object sender, EventArgs e)
{
if (checkBox5.Checked)
{
//loop through these files
foreach (FileInfo file in tempPath.GetFiles())
{
//delete files in content
file.Delete();
}
}
}
You must delete Folder recursivly with set FileAttributes normal. 您必须使用设置为FileAttributes normal的文件递归删除Folder。
private static void DeleteAllFolderRecursive(DirectoryInfo yourBaseDir)
{
baseDir.Attributes = FileAttributes.Normal;
foreach (var childDir in baseDir.GetDirectories())
DeleteFolderRecursive(childDir);
foreach (var file in baseDir.GetFiles())
file.IsReadOnly = false;
baseDir.Delete(true);
}
And you call this : 而你称之为:
DirectoryInfo tempPath = new DirectoryInfo(@"C:\Users\" + Environment.UserName + @"\AppData\Local\Temp");
DeleteAllFolderRecursive(tempPath);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.