删除指定文件夹下所有文件
程序员文章站
2022-05-20 10:35:02
...
删除指定文件夹下所有文件
private static void DeepCleanupFile(string path)
{
System.IO.DirectoryInfo fileInfo = new DirectoryInfo(path);
//去除文件夹的只读属性
fileInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;
//去除文件的只读属性
System.IO.File.SetAttributes(path, System.IO.FileAttributes.Normal);
if (Directory.Exists(path))
{
foreach (string f in Directory.GetFileSystemEntries(path))
{
if (File.Exists(f))
{
//删除文件
File.Delete(f);
}
else
{
//循环删除文件夹
DeepCleanupFile(f);
}
}
Directory.Delete(path);
}
}
上一篇: 印度12岁最小"道德黑客" 4岁会写程序
下一篇: 102. 二叉树的层次遍历