1

我做了一个可以扫描ftp服务器的方法。它在 if...else 语句中有两个部分。if 中的第一部分在目录列表等于 0 时运行,并将文件夹与文件分开(并将它们放入该列表中)。然后(因为列表不再为空)else 语句应该运行。它有一个 foreach 循环,它检查列表的所有元素并将它们连接到 ftp 地址并扫描该文件夹。这就是问题所在。看起来它变成了一个无限循环。我只想检查服务器上的文件夹并打破循环,但看起来我找不到有用的解决方案。

这是代码:

internal void ListFilesOnServer()
        {
            ArrayList files = new ArrayList();
            if (directories.Count == 0)
            {
                try
                {
                    FtpWebRequest ftpwrq = (FtpWebRequest)WebRequest.Create(server);
                    ftpwrq.Credentials = new NetworkCredential(user, passw);
                    ftpwrq.Method = WebRequestMethods.Ftp.ListDirectory;
                    ftpwrq.KeepAlive = false;
                    FtpWebResponse fresponse = (FtpWebResponse)ftpwrq.GetResponse();
                    StreamReader sr = new StreamReader(fresponse.GetResponseStream());
                    string temp = "";
                    while ((temp = sr.ReadLine()) != null)
                    {
                        files.Add(temp);
                    }
                    temp = String.Empty;
                    sr.Close();
                    fresponse.Close();
                    DirOrFile(files);
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }
            else
            {
                foreach (string file in directories.ToArray())
                {
                    try
                    {
                        FtpWebRequest ftpwrq = (FtpWebRequest)WebRequest.Create(server+"/"+file);
                        ftpwrq.Credentials = new NetworkCredential(user, passw);
                        ftpwrq.Method = WebRequestMethods.Ftp.ListDirectory;
                        ftpwrq.KeepAlive = false;
                        FtpWebResponse fresponse = (FtpWebResponse)ftpwrq.GetResponse();
                        StreamReader sr = new StreamReader(fresponse.GetResponseStream());
                        string temp = "";
                        while ((temp = sr.ReadLine()) != null)
                        {
                            files.Add(temp);
                        }
                        temp = String.Empty;
                        sr.Close();
                        fresponse.Close();
                        DirOrFile(files);
                    }
                    catch(ArgumentException)
                    {

                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message);
                    }
                }
            }
            ListFilesOnServer();
        }
4

1 回答 1

0

无限循环是因为你没有办法摆脱递归。

递归模式如下所示

MyRecursiveMethod()
{
    if (conditions)
    {
    }
    else
    {
        MyRecursiveMethod()
    }
}

以下是我可能会如何重写此代码以使其适合您的方法。

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Security;
using System.Windows.Forms;

namespace ConsoleApplication5
{
    public class FtpTest
    {
        string server = "servier/";
        string user = "user";
        SecureString passw = new SecureString();

        public List<string> GetFilesOnServer()
        {
            return GetFilesOnServer(server);
        }

        public List<string> GetFilesOnServer(string dir)
        {
            var root = GetDirectoryContents(dir);
            var files = new List<string>();

            foreach (string name in root)
            {
                var path = GetFullPath(dir, name);
                if (IsDirectory(name))
                {
                    var subFiles = GetFilesOnServer(path);
                    files.AddRange(subFiles);
                }
                else
                {
                    files.Add(path);
                }
            }

            return files;
        }

        public List<string> GetDirectoryContents(string dir)
        {
            var files = new List<string>();

            try
            {
                var ftpwrq = (FtpWebRequest)WebRequest.Create(dir);
                ftpwrq.Credentials = new NetworkCredential(user, passw);
                ftpwrq.Method = WebRequestMethods.Ftp.ListDirectory;
                ftpwrq.KeepAlive = false;
                var fresponse = (FtpWebResponse)ftpwrq.GetResponse();
                var sr = new StreamReader(fresponse.GetResponseStream());
                string fileName = "";
                while ((fileName = sr.ReadLine()) != null)
                {
                    files.Add(fileName);
                }
                sr.Close();
                fresponse.Close();
                return files;
            }
            catch (ArgumentException)
            {

            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

            return files;
        }

        public static string GetFullPath(string dir, string file)
        {
            string path = dir;
            if (!path.EndsWith("/"))
                path += "/";
            path += file;
            return path;
        }

        public static bool IsDirectory(string name)
        {
            return name.IndexOf(".") > 0;
        }
    }
}

请注意,如果检索到的项目是目录,我只会递归调用 GetFilesOnServer。我还将获取 FTP 服务器上的内容的代码重构为非递归方法。

希望这可以帮助你。

于 2013-06-23T17:14:38.530 回答