2014-12-07 70 views
0

我嘗試「創建」遞歸方法,它將ftp目錄列表複製到treeView。
我已經盡力做到這一點,但它更加快速&髒如清潔&簡單。

在這裏你可以看到我的代碼片段:
遞歸方法獲取ftp目錄列表

public void connectToServer(string pServerIP, string pServerPort, string pUsername, string pPassword) 
    { 
     _serverIP = pServerIP; 
     _serverPort = pServerPort; 
     _username = pUsername; 
     _password = pPassword; 

     string ftpServerPath = "ftp://" + pServerIP + ":" + pServerPort + "/"; 
     try 
     { 
      FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServerPath); 
      request.Method = WebRequestMethods.Ftp.ListDirectory; 
      request.Credentials = new NetworkCredential(pUsername, pPassword); 
      FtpWebResponse response = (FtpWebResponse)request.GetResponse(); 
      Stream responseStream = response.GetResponseStream(); 
      StreamReader reader = new StreamReader(responseStream); 

      secondLevelDirectotyList = new List<string>(); 

      int i = 0; 
      TreeNode rootTreeNode = tVDirectories.Nodes.Add("/"); 
      Console.WriteLine("/\n"); 
      while (!reader.EndOfStream) 
      { 
       secondLevelDirectotyList.Add(reader.ReadLine()); 
       Console.WriteLine("...: " + secondLevelDirectotyList[i]); 
       i++; 
      } 

      reader.Close(); 
      response.Close(); 

      getFTPDirectoryList(secondLevelDirectotyList, 0); 
     } 
     catch (WebException ex) 
     { 
      MessageBox.Show("The following Exceptions occurs:\n" + ex.Message, "Exception occurs", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     } 
    } 

    private void getFTPDirectoryList(List<string> pTopLevelDirectoryList, int pDirectoryListIndexer)//string pFTPPath) 
    { 
     //List<string> 
     string ftpServerPath = "ftp://" + _serverIP + ":" + _serverPort + "/" + pTopLevelDirectoryList[pDirectoryListIndexer];//pFTPPath; 
     try 
     { 
      FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServerPath); 
      request.Method = WebRequestMethods.Ftp.ListDirectory; 
      request.Credentials = new NetworkCredential(_username, _password); 
      FtpWebResponse response = (FtpWebResponse)request.GetResponse(); 
      Stream responseStream = response.GetResponseStream(); 
      StreamReader reader = new StreamReader(responseStream); 

      thirdLevelDirectoryList = new List<string>(); 

      int i = 0; 
      TreeNode ftpServerDirectory = tVDirectories.Nodes[0].Nodes.Add(pTopLevelDirectoryList[pDirectoryListIndexer]);//pFTPPath); 
      while (!reader.EndOfStream) 
      { 
       string streamFTPPath = reader.ReadLine(); //Ließt die Zeile des Streams aus 
       thirdLevelDirectoryList.Add(streamFTPPath); //Fügt den gesamten Pfad in die String-Liste 
       Console.WriteLine("...........: " + thirdLevelDirectoryList[i]); 

       string newTreeNode = streamFTPPath.Substring(streamFTPPath.IndexOf(@"/") + 1); 
       ftpServerDirectory.Nodes.Add(newTreeNode); //Fügt nur den Unterordner- oder Unterdatei-Namen in die Ansicht ein 
       i++; 
      } 

      reader.Close(); 
      response.Close(); 

      //rekursiv 
      pDirectoryListIndexer++; 
      try 
      {      
       getFTPDirectoryList(pTopLevelDirectoryList, pDirectoryListIndexer); 
      } 
      catch (ArgumentOutOfRangeException ex) 
      { 
       // start next level Directory List 
       //pDirectoryListIndexer = 0; 
       //getFTPDirectoryList(thirdLevelDirectoryList, 0); 
      } 
     } 
     catch (WebException ex) 
     { 
      MessageBox.Show("The following Exceptions occurs:\n" + ex.Message, "Exception occurs", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     } 
    } 

正如你所看到的,我沒有得到一點通過FTP文件夾瀏覽。
您也可能會注意到我第一次使用ftp協議。
例如:我不知道每次發送ftp-ListDirectory-Command時是否應該打開一個請求。

我想是這樣的:
根/
...文件夾1
...... file1infolder1
...文件2
...文件夾2
...... file3infolder2

我希望你能理解我:D
我也是因爲我的英語不好。

+0

我明白了。明天我會發布解決方案,但我認爲遞歸GetFTPDirectoryList方法效率不高,因爲檢查服務器或NAS上的每個文件需要很長時間。所以我會改變它。我認爲解決這個問題的最好方法是在用戶點擊它時檢查一個文件夾。 – Ismoh 2014-12-08 21:01:53

回答

0

這是我的一個遞歸方法,它從一個FTP路徑列出所有文件和文件夾的解決方案,

效率不高,這是不可能使用!!!!
如果你只有五個文件夾,這種方法將工作,但如果你有超過〜五個文件夾的方法將不會結束 - 確保它會完成某一天..

因此,每個人,誰讀這個:
想想你的想法,使用遞歸方法列出ftp目錄!
一旦用戶「打開」文件夾,最好應發送NLIST-ftp-command。

private void FtpNlistRecursive(string pPath) 
    { 
     try 
     { 
      DirectoryListOfCurrent = new List<string>(); 
      _ftpServerFullPath = "ftp://" + _serverIP + ":" + _serverPort + "/" + pPath; 
      string newItem = ""; 

      FtpWebRequest request = (FtpWebRequest)WebRequest.Create(_ftpServerFullPath); 
      request.Method = WebRequestMethods.Ftp.ListDirectory; 
      request.Credentials = new NetworkCredential(_username, _password); 
      FtpWebResponse response = (FtpWebResponse)request.GetResponse(); 
      Stream responseStream = response.GetResponseStream(); 
      StreamReader reader = new StreamReader(responseStream); 

      while (!reader.EndOfStream) 
      { 
       newItem = reader.ReadLine(); 
       string shortItem = pPath.Substring(pPath.IndexOf(@"/") + 1); // Aus "Ornder1/Datei1.txt" wird "Datei1.txt" 
       if (!shortItem.Equals(newItem)) 
       { 
        try 
        { 
         if (pPath.Equals("/")) 
         { 
          DirectoryListOfCurrent.Add(newItem); 
          directoryListOfAll.Add(newItem); 
         } 
         else 
         { 
          string completePath = pPath + newItem.Substring(newItem.IndexOf(@"/")); 
          DirectoryListOfCurrent.Add(completePath); 
          directoryListOfAll.Add(completePath); 
         } 
        } 
        catch (ArgumentOutOfRangeException ex) 
        { 
         //bei ZB "Datei3.txt" gibt es kein "/", somit einfach ignorieren 
        } 

       } 
      } 
      reader.Close(); 
      response.Close(); 

      foreach (string item in DirectoryListOfCurrent) 
      { 
       FtpNlistRecursive(item); 
      } 
     } 
     catch (Exception ex) 
     { 
      ExceptionOccurs(ex); 
     } 
    }