C#删除文件夹
我们知道在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);
}
}
采用递归的方式,先删除文件夹中的文件,然后删除空文件夹。
摘自 白杨树
补充:软件开发 , C# ,