C#删除文件夹及文件(三种方法)

1这是最简单的方法
 
        DirectoryInfo di = new DirectoryInfo(string Path);
         di.Delete(true);
  注: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
                 }  
           }
     }
}
你可以使用C#的FTP类库,如`System.Net.FtpWebRequest`和`System.Net.FtpWebResponse`来删除FTP服务器上的文件夹。 以下是一个示例代码,可以删除FTP服务器上的文件夹及其所有子文件和子文件夹: ```csharp public void DeleteDirectory(string serverUri, string username, string password, string directoryToDelete) { FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(serverUri + "/" + directoryToDelete); ftpRequest.Credentials = new NetworkCredential(username, password); ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails; List<string> allFilesAndFoldersInDirectory = new List<string>(); using (FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse()) { using (Stream responseStream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(responseStream)) { while (!reader.EndOfStream) { string currentFileOrFolder = reader.ReadLine(); allFilesAndFoldersInDirectory.Add(currentFileOrFolder); } } } } foreach (string fileOrFolder in allFilesAndFoldersInDirectory) { string currentFileOrFolderPath = serverUri + "/" + directoryToDelete + "/" + fileOrFolder; if (fileOrFolder != "." && fileOrFolder != "..") { if (fileOrFolder.Contains(".")) { //Delete file ftpRequest = (FtpWebRequest)WebRequest.Create(currentFileOrFolderPath); ftpRequest.Credentials = new NetworkCredential(username, password); ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile; using (FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse()) { //Do nothing, just delete } } else { //Delete folder DeleteDirectory(serverUri, username, password, directoryToDelete + "/" + fileOrFolder); } } } //Delete the root folder ftpRequest = (FtpWebRequest)WebRequest.Create(serverUri + "/" + directoryToDelete); ftpRequest.Credentials = new NetworkCredential(username, password); ftpRequest.Method = WebRequestMethods.Ftp.RemoveDirectory; using (FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse()) { //Do nothing, just delete } } ``` 你可以像这样调用`DeleteDirectory`方法: ```csharp DeleteDirectory("ftp://example.com", "username", "password", "folderToDelete"); ``` 注意替换`serverUri`、`username`、`password`和`directoryToDelete`为你自己的FTP服务器信息和要删除文件夹路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值