File.Delete( string fullName );
删除文件夹需要 递归来操作,就是如果文件夹不是空的,就要删除里边所有文件和文件夹,然后才能删除本文件夹。
/// <summary>
/// 删除目录。
/// </summary>
/// <param name="fullPath"></param>
private void DeleteDir( string fullPath )
{
if ( Directory.Exists( fullPath ) )
{
DirectoryInfo dir = new DirectoryInfo( fullPath );
FileInfo[] files = dir.GetFiles();
if ( files != null )
{
foreach ( FileInfo file in files )
{
File.Delete( file.FullName );
}
}
DirectoryInfo[] directories = dir.GetDirectories();
if ( directories != null )
{
foreach ( DirectoryInfo directory in directories )
{
DeleteDir( directory.FullName );
}
}
try
{
Directory.Delete( dir.FullName );
}
catch ( Exception ex )
{
}
}
删除文件夹需要 递归来操作,就是如果文件夹不是空的,就要删除里边所有文件和文件夹,然后才能删除本文件夹。
/// <summary>
/// 删除目录。
/// </summary>
/// <param name="fullPath"></param>
private void DeleteDir( string fullPath )
{
if ( Directory.Exists( fullPath ) )
{
DirectoryInfo dir = new DirectoryInfo( fullPath );
FileInfo[] files = dir.GetFiles();
if ( files != null )
{
foreach ( FileInfo file in files )
{
File.Delete( file.FullName );
}
}
DirectoryInfo[] directories = dir.GetDirectories();
if ( directories != null )
{
foreach ( DirectoryInfo directory in directories )
{
DeleteDir( directory.FullName );
}
}
try
{
Directory.Delete( dir.FullName );
}
catch ( Exception ex )
{
}
}
本文介绍了一种删除文件夹的递归方法,包括先删除文件再删除文件夹的过程,确保了目录的完全清除。

被折叠的 条评论
为什么被折叠?



