Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何修复以下方法?_C#_Loops_Ftp - Fatal编程技术网

C# 如何修复以下方法?

C# 如何修复以下方法?,c#,loops,ftp,C#,Loops,Ftp,我做了一个可以扫描ftp服务器的方法。它在if…else语句中有两个部分。当目录列表等于0时,if中的第一部分将运行,并将文件夹与文件分开,然后将它们放入该列表中。然后,由于列表不再为空,因此应该运行else语句。它有一个foreach循环,检查列表中的所有元素,将它们连接到ftp地址并扫描该文件夹。这就是问题所在。看起来它变成了一个无限循环。我只想检查服务器上的文件夹并打破循环,但似乎找不到有用的解决方案 代码如下: internal void ListFilesOnServer()

我做了一个可以扫描ftp服务器的方法。它在if…else语句中有两个部分。当目录列表等于0时,if中的第一部分将运行,并将文件夹与文件分开,然后将它们放入该列表中。然后,由于列表不再为空,因此应该运行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();
        }

无限循环是因为你没有办法打破递归

递归模式如下所示

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服务器上内容的代码重构为非递归方法


希望这对您有所帮助。

它在哪里卡住了?您是否使用断点进行了检查?else语句不会在if之后运行,只需让代码跟随即可。if语句如果希望发生这种情况,则不会陷入循环中,因为您在不设置目录的情况下递归调用函数