這裏說三種
1這是最簡單的方法
DirectoryInfo di = new DirectoryInfo(string Path);
di.Delete(true);spa
注:path是你要刪除的非空目錄;
true:你要刪除裏面全部的文件,包括文件夾和子文件夾
2
/// <summary>
/// 刪除非空文件夾
/// </summary>
/// <param name="path">要刪除的文件夾目錄</param>
void DeleteDirectory(string path)
{
DirectoryInfo dir = new DirectoryInfo(path);
if (dir.Exists)
{
DirectoryInfo[] childs = dir.GetDirectories();
foreach (DirectoryInfo child in childs)
{
child.Delete(true);
}
dir.Delete(true);
}
}
3 遞歸方法:(這是刪除文件的方法)
private staticv oid DeleteDirectory(File tmpFile) {
if (!tmpFile.exists()) {
System.out.println("file is not exist!");
return;
}
if (tmpFile.isDirectory()) {
File[] fileList = tmpFile.listFiles();
for (int i =0; i < fileList.length; i++) {
if (fileList[i].isDirectory()) {
DeleteDirectorys(fileList[i]);s
}
}
}
}遞歸