2013-09-16 64 views
1

我已經搜索了網頁,並且對此仍然陌生。請耐心等待我。C#使用FTP服務器目錄填充TreeView

我想要做的是: - 我有一個名爲「treeTO」的TreeView。 - 我連接到我的FTP服務器,我想在樹視圖中填充這裏的所有內容(目錄,子目錄和文件,無論它是什麼)。 - 這是我的代碼至今:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + ActiveServer.Server + "/"); 
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails; 
request.Credentials = new NetworkCredential(ActiveServer.UserName, ActiveServer.Password); 
FtpWebResponse response = (FtpWebResponse)request.GetResponse(); 
Stream responseStream = response.GetResponseStream(); 
StreamReader reader = new StreamReader(responseStream); 

- 這將返回我下面的文字:日誌,數據和wwwroot文件。

我很努力將其添加到樹視圖中,然後遍歷文件夾以將所有內容添加到樹視圖。

+1

你使用哪種TECHNOLOGIE? ASP.NET,WPF,WinForms?和哪個TreeView? – makim

+0

它的WinForms,和它的正常TreeView與節點旁邊的加號 –

+0

好吧我會看看,並會發表一個答案;-) – makim

回答

3

所以這裏是我爲您工作的解決方案!

當然,您必須用您的FTP服務器的rootURL替換「root」的值,並在方法GetWebRequest()中用您的憑證替換「用戶名」和「密碼」!

你需要這個類來保存詳細

public class FTPListDetail 
{ 
     public bool IsDirectory 
     { 
      get 
      { 
       return !string.IsNullOrWhiteSpace(Dir) && Dir.ToLower().Equals("d"); 
      } 
     } 
     internal string Dir { get; set; } 
     public string Permission { get; set; } 
     public string Filecode { get; set; } 
     public string Owner { get; set; } 
     public string Group { get; set; } 
     public string Name { get; set; } 
     public string FullPath { get; set; } 
} 

,這裏是代碼隱藏您的WinForm程序內置

private void button1_Click(object sender, EventArgs e) 
{ 
    var root = "ftp://ftp.yourFTPServer.at"; 

    treeView1.Nodes.Clear(); 
    treeView1.Nodes.Add(CreateDirectoryNode(root, "root")); 
} 

private TreeNode CreateDirectoryNode(string path, string name) 
{ 
    var directoryNode = new TreeNode(name); 
    var directoryListing = GetDirectoryListing(path); 

    var directories = directoryListing.Where(d => d.IsDirectory); 
    var files = directoryListing.Where(d => !d.IsDirectory); 

    foreach (var dir in directories) 
    { 
     directoryNode.Nodes.Add(CreateDirectoryNode(dir.FullPath, dir.Name)); 
    } 
    foreach (var file in files) 
    { 
      directoryNode.Nodes.Add(new TreeNode(file.Name)); 
    } 
    return directoryNode; 
} 

public IEnumerable<FTPListDetail> GetDirectoryListing(string rootUri) 
{ 
    var CurrentRemoteDirectory = rootUri; 
    var result = new StringBuilder(); 
    var request = GetWebRequest(WebRequestMethods.Ftp.ListDirectoryDetails, CurrentRemoteDirectory); 
    using (var response = request.GetResponse()) 
    { 
     using (var reader = new StreamReader(response.GetResponseStream())) 
     { 
      string line = reader.ReadLine(); 
      while (line != null) 
      { 
       result.Append(line); 
       result.Append("\n"); 
       line = reader.ReadLine(); 
      } 
      if (string.IsNullOrEmpty(result.ToString())) 
      { 
       return new List<FTPListDetail>(); 
      } 
      result.Remove(result.ToString().LastIndexOf("\n"), 1); 
      var results = result.ToString().Split('\n'); 
      string regex = 
       @"^" +    //# Start of line 
       @"(?<dir>[\-ld])" +   //# File size   
       @"(?<permission>[\-rwx]{9})" +   //# Whitespace   \n 
       @"\s+" +   //# Whitespace   \n 
       @"(?<filecode>\d+)" + 
       @"\s+" +   //# Whitespace   \n 
       @"(?<owner>\w+)" + 
       @"\s+" +   //# Whitespace   \n 
       @"(?<group>\w+)" + 
       @"\s+" +   //# Whitespace   \n 
       @"(?<size>\d+)" + 
       @"\s+" +   //# Whitespace   \n 
       @"(?<month>\w{3})" +   //# Month (3 letters) \n 
       @"\s+" +   //# Whitespace   \n 
       @"(?<day>\d{1,2})" +  //# Day (1 or 2 digits) \n 
       @"\s+" +   //# Whitespace   \n 
       @"(?<timeyear>[\d:]{4,5})" +  //# Time or year  \n 
       @"\s+" +   //# Whitespace   \n 
       @"(?<filename>(.*))" +   //# Filename   \n 
       @"$";    //# End of line 

      var myresult = new List<FTPListDetail>(); 
      foreach (var parsed in results) 
      { 
       var split = new Regex(regex) 
        .Match(parsed); 
       var dir = split.Groups["dir"].ToString(); 
       var permission = split.Groups["permission"].ToString(); 
       var filecode = split.Groups["filecode"].ToString(); 
       var owner = split.Groups["owner"].ToString(); 
       var group = split.Groups["group"].ToString(); 
       var filename = split.Groups["filename"].ToString(); 
       myresult.Add(new FTPListDetail() 
       { 
        Dir = dir, 
        Filecode = filecode, 
        Group = group, 
        FullPath = CurrentRemoteDirectory + "/" + filename, 
        Name = filename, 
        Owner = owner, 
        Permission = permission, 
       }); 
      }; 
      return myresult; 
     } 
    } 
} 

private FtpWebRequest GetWebRequest(string method, string uri) 
{ 
    Uri serverUri = new Uri(uri); 
    if (serverUri.Scheme != Uri.UriSchemeFtp) 
    { 
     return null; 
    } 
    var reqFTP = (FtpWebRequest)FtpWebRequest.Create(serverUri); 
    reqFTP.Method = method; 
    reqFTP.UseBinary = true; 
    reqFTP.Credentials = new NetworkCredential("yourUser", "yourPassword"); 
    reqFTP.Proxy = null; 
    reqFTP.KeepAlive = false; 
    reqFTP.UsePassive = false; 
    return reqFTP; 
} 
+0

哇,這太神奇了。它的工作完美!謝謝:)! –

+0

np,歡迎您:-) – makim

1

爲什麼地球上沒有FTP客戶端搶先在FTP服務器上填充樹視圖或在樹視圖中顯示文件圖標是有原因的。其中任何一個都會帶走。

你需要做的是重新思考你是如何做到這一點的。當你第一次連接時,你只想填充樹,直到最初的目錄和最初的目錄。你不想把文件放到你的樹形視圖中,因爲在許多現實世界的情況下,這也會消耗太多的內存。只需添加文件夾(並使用相同的ICON> 99%的那些)。

這是它應該是什麼樣子,當你第一次登錄:

 
/
-home 
-joe 
    -public_html 
    -docs 
    -uploads 

注意,「/」和「家」還沒有完全在這一點上閱讀,我們只是把它們插入作爲的佔位符用戶點擊。之後,您只需要在單擊文件夾中添加目錄,這些目錄在用戶每次單擊文件夾時都不在其中。

這種策略將使生活變得更容易。即使你想嘗試一些更復雜的方法來處理這個問題,你也需要從基礎知識入手。