递归方法获取 ftp 目录列表

本文关键字:列表 ftp 获取 递归方法 | 更新日期: 2023-09-27 18:33:23

我尝试"创建"一个递归方法,该方法将ftp目录列表复制到treeView。
我都准备好了,但它更快速和肮脏,因为干净和简单。

在这里你可以看到我的代码片段:

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
......文件1在文件夹中1
...文件2
...文件夹2
......文件3文件夹2

我希望你能理解我:D
我也为我的英语不好而感到不满。

递归方法获取 ftp 目录列表

这是我对递归方法的解决方案,该方法列出了 ftp 路径中的所有文件和文件夹,

效率低下,无法使用!!!
如果你只有五个文件夹,这个方法会起作用,但如果你有超过~五个文件夹,该方法不会结束 - 肯定有一天它会完成。

所以对于每个读到这篇文章的人:
想想你的想法,使用递归方法来列出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);
        }
    }