我們知道在c#中如果一個文件夾中有內容的話,直接使用Directory.Delete(文件夾);是刪不掉的,那麼如何進行刪除?下面這個小方法可以幫助你。
view plaincopy to clipboardprint?
public void DeleteFolder(string deleteDirectory)
{
if (Directory.Exists(deleteDirectory))
{
foreach(string deleteFile in Directory.GetFileSystemEntries(deleteDirectory))
{
if(File.Exists(deleteFile ))
File.Delete(deleteFile );
else
DeleteFolder(deleteFile );
}
Directory.Delete(deleteDirectory);
}
}
采用遞歸的方式,先刪除文件夾中的文件,然後刪除空文件夾。
public void DeleteFolder(string deleteDirectory)
{
if (Directory.Exists(deleteDirectory))
{
foreach(string deleteFile in Directory.GetFileSystemEntries(deleteDirectory))
{
if(File.Exists(deleteFile ))
File.Delete(deleteFile );
else
DeleteFolder(deleteFile );
}
Directory.Delete(deleteDirectory);
}
}
采用遞歸的方式,先刪除文件夾中的文件,然後刪除空文件夾。
摘自 白楊樹