我该如何修复以下方法?

时间:2013-06-23 15:24:57

标签: c# loops ftp

我制作了一个可以扫描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();
        }

1 个答案:

答案 0 :(得分: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服务器上的内容抓取成非递归方法的代码。

希望这可以帮助你。