ControlFileClass类中的DeleteFolder函数:
/// <summary>
/// 删除指定文件夹
/// </summary>
/// <param name="dir">文件夹路径</param>
public static void DeleteFolder(string dirPath)
{
foreach (string d in Directory.GetFileSystemEntries(dirPath))
{
if (File.Exists(d))
{
FileInfo fi = new FileInfo(d);
if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
fi.Attributes = FileAttributes.Normal;
File.Delete(d);//直接删除其中的文件
}
else
{
DeleteFolder(d);//递归删除子文件夹
}
}//end of for
Directory.Delete(dirPath);//删除已空文件夹
}//end of DeleteFolder
删除操作:
ControlFileClass test = new ControlFileClass(rootPath,false);
ArrayList x = test.FileListName;
for (int i = 0; i < x.Count; i++)
{
if (ControlFileClass.IsFolder(rootPath + @"\" +x[i]))
{
Console.WriteLine(rootPath + @"\" + x[i]);
ControlFileClass.DeleteFolder(rootPath + @"\" + x[i]);
Console.WriteLine("删除文件夹:" + x[i]);
}
}