2015-04-18 68 views
-1

我正在使用Visual Studio 2013中的C#(Winform)編寫更新程序。我想下載FTP文件和文件夾遞歸,因爲該文件夾包含我的遠程更新文件。如何獲取FTP文件名和文件夾名?

我的CS:

#region 遞歸文件夾 
    /// <summary> 
    /// 遞歸下載文件 
    /// </summary> 
    /// <param name="ftpIPAddress">FTP服務器的IP</param> 
    /// <param name="name">需要下載文件路徑</param> 
    /// <param name="localPath">保存的本地路徑</param> 
    public void downFTP(string ftpIPAddress, string name, string localPath) 
    { 
     string downloadDir = localPath + name; 
     string ftpDir = ftpIPAddress + "/" + name; 
     string[] fullname = FTP(ftpDir, WebRequestMethods.Ftp.ListDirectoryDetails); 
     //判斷是否爲單個文件 
     if (fullname.Length <= 2) 
     { 
      string singleFullFileName = fullname[fullname.Length - 1]; 
      if (!string.IsNullOrEmpty(singleFullFileName)) 
      { 
       string FTPFileName = GetFolderNameByFTPDirectoryDetail(singleFullFileName); 
       FTPDownloadFile(ftpIPAddress, userName, passWord, localPath, FTPFileName, FTPFileName); 
      } 
      else 
      { 
       logger.Error("下載FTP文件時,文件名爲空,請檢查,fullname:" + singleFullFileName); 
      } 
     } 
     else 
     { 
      string[] onlyname = FTP(ftpDir, WebRequestMethods.Ftp.ListDirectory); 
      if (!Directory.Exists(downloadDir)) 
      { 
       Directory.CreateDirectory(downloadDir); 
      } 
      foreach (string names in fullname) 
      { 
       //判斷是否具有文件夾標識<DIR> 
       if (names.Contains("<DIR>")) 
       { 
        string olname = names.Split(new string[] { "<DIR>" }, StringSplitOptions.None)[1].Trim(); 
        //如果爲文件夾,在temp目錄下創建相同結構的文件夾 
        string FTPFolderName = GetFolderNameByFTPDirectoryDetail(names); 
        if (!string.IsNullOrEmpty(FTPFolderName)) 
        { 
         PreUpdate(FTPFolderName); 
         //如果爲文件夾,遞歸下載 
         downFTP(ftpDir, "//" + olname, downloadDir); 
        } 
        else 
        { 
         logger.Info("獲取到空的文件夾名稱"); 
        } 
       } 
       else 
       { 
        foreach (string onlynames in onlyname) 
        { 
         if (onlynames == "" || onlynames == " " || names == "") 
         { 
          break; 
         } 
         else 
         { 
          if (names.Contains(" " + onlynames)) 
          { 
           //DownloadFile(downloadDir + "/" + onlynames, ftpAddr + name + "/" + onlynames); 
           FTPDownloadFile(ftpIPAddress, userName, passWord, localPath, onlynames, onlynames); 
           logger.Info("下載文件,下載存儲位置:" + downloadDir + ",FTP位置:" + ftpDir); 
           break; 
          } 
         } 
        } 
       } 
      } 
     } 

    } 
    #endregion 

你可以看到,該函數FTPDownloadFile需要filename參數。

,我可以得到像

04-17-15 07:21PM     2 a.txt 

的FTP目錄細節這是我的方式來獲取文件名或文件夾名稱:

 #region 得到文件夾的名稱 
     public string GetFolderNameByFTPDirectoryDetail(string FTPDirectoryDetails) 
     { 
      string folderName = "";    
      logger.Info("文件的詳細路徑爲:"+FTPDirectoryDetails); 
      string[] folderNameArr = FTPDirectoryDetails.Split(new string[] { " "},StringSplitOptions.RemoveEmptyEntries); 
      int arrLength = folderNameArr.Length; 
      folderName = folderNameArr[arrLength-1]; 
      logger.Info("獲取FTP文件夾的名稱爲:" + folderName); 
      return folderName; 
     } 
     #endregion 

而這個功能是如此的脆弱,因爲文件夾名稱,文件名可能包含''。

如何獲取文件名和文件夾名稱?

回答

1
/// <summary> 
/// 從ftp服務器上獲得文件夾列表 
/// </summary> 
/// <param name="RequedstPath">服務器下的相對路徑</param> 
/// <returns></returns> 
public static List<string> GetDirctory(string RequedstPath) 
{ 
    List<string> strs = new List<string>(); 
    try 
    { 
     string uri = path + RequedstPath; //目標路徑 path爲服務器地址 
     FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri)); 
     // ftp用戶名和密碼 
     reqFTP.Credentials = new NetworkCredential(username, password); 
     reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails; 
     WebResponse response = reqFTP.GetResponse(); 
     StreamReader reader = new StreamReader(response.GetResponseStream());//中文文件名 

     string line = reader.ReadLine(); 
     while (line != null) 
     { 
      if (line.Contains("<DIR>")) 
      { 
       string msg = line.Substring(line.LastIndexOf("<DIR>")+5).Trim(); 
       strs.Add(msg); 
      } 
      line = reader.ReadLine(); 
     } 
     reader.Close(); 
     response.Close(); 
     return strs; 
    } 
    catch (Exception ex) 
    { 
     Console.WriteLine("獲取目錄出錯:" + ex.Message); 
    } 
    return strs; 
} 

您可以參考:http://www.cnblogs.com/zhangjun1130/archive/2010/03/24/1693932.html

相關問題