public void DeleteSrcFolder(string file)
{
//去除文件夹和子文件的只读属性
//去除文件夹的只读属性
System.IO.DirectoryInfo fileInfo = new DirectoryInfo(file);
fileInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;
//去除文件的只读属性
System.IO.File.SetAttributes(file, System.IO.FileAttributes.Normal);
//判断文件夹是否还存在
if (Directory.Exists(file))
{
foreach (string f in Directory.GetFileSystemEntries(file))
{
if (File.Exists(f))
{
//如果有子文件删除文件
File.Delete(f);
}
else
{
//循环递归删除子文件夹
DeleteSrcFolder1(f);
}
}
//删除空文件夹
//Directory.Delete(file);
}
}
public void DeleteSrcFolder1(string file)
{
//去除文件夹和子文件的只读属性
//去除文件夹的只读属性
System.IO.DirectoryInfo fileInfo = new DirectoryInfo(file);
fileInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;
//去除文件的只读属性
System.IO.File.SetAttributes(file, System.IO.FileAttributes.Normal);
//判断文件夹是否还存在
if (Directory.Exists(file))
{
foreach (string f in Directory.GetFileSystemEntries(file))
{
if (File.Exists(f))
{
//如果有子文件删除文件
File.Delete(f);
}
else
{
//循环递归删除子文件夹
DeleteSrcFolder1(f);
}
}
//删除空文件夹
Directory.Delete(file);
}
}
本文介绍了一个使用C#编写的函数,用于删除指定路径下的文件夹及其所有子文件和子文件夹。首先,函数会移除文件夹和文件的只读属性,然后检查并递归地删除所有子文件和子文件夹,最后尝试删除空文件夹。

4348

被折叠的 条评论
为什么被折叠?



