寻找ftp地址下所有文件的代码,包括子文件夹(c#)

  1.         private string key1 = "File";
  2.         private string key2 = "Folder";
  3.         private List<string> alreadyPathed = new List<string>();
  4.         private Dictionary<string, List<string>> getPath(string ftpURL, Dictionary<string, List<string>> retDic)
  5.         {
  6.             
  7.             List<string> listValueFile;
  8.             List<string> listValueFolder;
  9.             if (retDic == null)
  10.             {
  11.                 retDic = new Dictionary<string, List<string>>();
  12.                 listValueFile = new List<string>();
  13.                 listValueFolder = new List<string>();
  14.                 retDic.Add(key1, listValueFile);
  15.                 retDic.Add(key2, listValueFolder);
  16.             }
  17.             else
  18.             {
  19.                 listValueFile = retDic[key1];
  20.                 listValueFolder = retDic[key2];
  21.             }
  22.             string ftpUrl = ftpURL;
  23.             alreadyPathed.Add(ftpUrl);
  24.           NetworkCredential nc;
  25.             if(_config.FTPUserName == "")
  26.                 nc = new NetworkCredential();
  27.             else
  28.                 nc  =  new NetworkCredential(_config.FTPUserName, _config.FTPPassword);
  29.             WebRequest request = FtpWebRequest.Create(ftpUrl);
  30.             request.Credentials = nc;
  31.             request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
  32.             WebResponse response = request.GetResponse();
  33.             StreamReader sr = new StreamReader(response.GetResponseStream());
  34.             string strDirectory = sr.ReadToEnd();
  35.             sr.Close();
  36.             response.Close();
  37.             string[] strFiles = strDirectory.Split(new string[] { "/r/n" }, StringSplitOptions.RemoveEmptyEntries);
  38.             for (int i = 0; i < strFiles.Length; i++)
  39.             {
  40.                 string tempStr = strFiles[i].Replace(" """);
  41.                 int tempInt = tempStr.IndexOf("<DIR>");
  42.                 string tempFolder = ftpUrl + "/";
  43.                 if (tempInt != -1)
  44.                 {
  45.                     listValueFolder.Add(tempFolder + tempStr.Substring(tempInt + 5));
  46.                 }
  47.                 else
  48.                 {
  49. //这里看各种文化不同,对时间的表示可能会有不同的格式,我的因为操作系统中是占去了17位,因此从17位开始取,后面
  50. 的就是文件名了(要取道还要再去掉空格前面的一部分无用信息)
  51.                     string tempString = strFiles[i].Substring(17).TrimStart(' ');
  52.                     int tempint = tempString.IndexOf(' ') + 1;
  53.                     listValueFile.Add(tempFolder + tempString.Substring(tempint));
  54.                                   }
  55.             }
  56.             for (int i = 0; i < listValueFolder.Count; i++)
  57.             {
  58.                 bool alreadyDone = alreadyPathed.Contains(listValueFolder[i]);
  59.                 if (!alreadyDone)
  60.                     getPath(listValueFolder[i], retDic);
  61.             }
  62.             return retDic;
  63.         }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用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、付费专栏及课程。

余额充值