您的位置:首页 > 其它

递归删除某一路径下的所有文件和文件夹

2015-03-30 13:46 567 查看
Private Function DelStoreFile(ByVal storeFilePath As String) As String
        Try
            Dim fileList As String() = Directory.GetFileSystemEntries(storeFilePath)

            For Each filePath In fileList
                If Directory.Exists(filePath) Then
                    Dim csvPath As String = DelStoreFile(filePath)
                    If csvPath IsNot Nothing Then
                        Return csvPath
                    Else
                        Directory.Delete(filePath)
                    End If
                Else
                    Dim fileInfo As New FileInfo(filePath)
                    If fileInfo.Exists Then
                        If (fileInfo.Attributes And FileAttributes.ReadOnly) = FileAttributes.ReadOnly Then
                            fileInfo.Attributes = System.IO.FileAttributes.Normal
                        End If

                        fileInfo.Delete()
                    End If
                End If
            Next
            Return Nothing
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

C#
using System.IO;  
using System.Linq;  
  
namespace FileFolderDeleter  
{  
    static class Program  
    {  
        static void Main(string[] args)  
        {  
            if (args.Count() == 1)  
            {  
                DeleteFilesAndFolders(args[0]);  
            }  
        }  
  
        /// <summary>   
        /// Recursively delete all the files and folders under the specific path.   
        /// </summary>   
        /// <param name="path">The specific path</param>   
        private static void DeleteFilesAndFolders(string path)  
        {  
            // Delete files.   
            string[] files = Directory.GetFiles(path);  
            foreach (var file in files)  
            {  
                File.Delete(file);  
            }  
  
            // Delete folders.   
            string[] folders = Directory.GetDirectories(path);  
            foreach (var folder in folders)  
            {  
                DeleteFilesAndFolders(folder);  
                Directory.Delete(folder);  
            }  
        }  
    }  
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: