public static void DelectDir(string srcPath)
{ try { DirectoryInfo dir = new DirectoryInfo(srcPath); FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目錄中所有文件和子目錄 foreach (FileSystemInfo i in fileinfo) { if (i is DirectoryInfo) //判斷是否文件夾 { DirectoryInfo subdir = new DirectoryInfo(i.FullName); subdir.Delete(true); //刪除子目錄和文件 } else { File.Delete(i.FullName); //刪除指定文件 } } } catch (Exception e) { throw; }
}
調用DelectDir方法前可以先判斷文件夾是否存在
if(Directory.Exists(srcPath)) { DelectDir(srcPath); }
原文地址:http://www.cnblogs.com/iamlucky/p/5997865.html