获取ftp directoryList的递归方法

时间:2014-12-07 17:05:16

标签: c# ftp treeview

我尝试“创建”一个递归方法,它将ftp目录列表复制到treeView。
我已经尝试过这样做,但它更快更快肮脏如清洁&简单。

在这里你可以看到我的Code-Snippets:

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-protocol 例如:我不知道每次发送ftp-ListDirectory-Command时是否应该打开请求
我想要这样的事情:
根/
...文件夹1
...... file1infolder1
...文件2
...文件夹2
...... file3infolder2

我希望你能理解我:D
而且我也因为我的英语不好而撒谎。

1 个答案:

答案 0 :(得分:0)

这是我的递归方法的解决方案,它从ftp路径列出所有文件和文件夹,
BUT
效率不高 ,无法使用!!!!
如果你只有五个文件夹,这个方法就可以了,但是如果你有超过五个文件夹,方法就不会结束 - 确保它将在某一天结束。

所以每个人都读到这个:
考虑一下你的想法,使用递归方法列出ftp目录!
一旦用户“打开”文件夹,您最好发送NLIST-ftp命令。

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);
        }
    }