1

我正在尝试使用StreamReader. 问题是它花费了太长时间并抛出*“操作超时异常”并且只显示一个级别。

这是我的代码

public void getServerSubfolder(TreeNode tv, string parentNode) {
  string ptNode;
  List<string> files = new List<string> ();
  try { 
    FtpWebRequest request = (FtpWebRequest) WebRequest.
    Create(parentNode);
    request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

    request.Credentials = new NetworkCredential(this.userName, this.Password);
    request.UseBinary = true;
    request.UsePassive = true;
    request.Timeout = 10000;
    request.ReadWriteTimeout = 10000;
    request.KeepAlive = false;
    FtpWebResponse response = (FtpWebResponse) request.GetResponse();
    Stream responseStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(responseStream);
    string fileList;
    string[] fileName;
    //MessageBox.Show(reader.ReadToEnd().ToString());
    while (!reader.EndOfStream) {
      fileList = reader.ReadLine();
      fileName = fileList.Split(' ');
      if (fileName[0] == "drwxr-xr-x") {
        // if it is directory
        TreeNode tnS = new TreeNode(fileName[fileName.Length - 1]);
        tv.Nodes.Add(tnS);
        ptNode = parentNode + "/" + fileName[fileName.Length - 1] + "/";
        getServerSubfolder(tnS, ptNode);
      } else files.Add(fileName[fileName.Length - 1]);
    }
    reader.Close();
    response.Close();
  } catch (Exception ex) {
    MessageBox.Show("Sub--here " + ex.Message + "----" + ex.StackTrace);
  }
}
4

2 回答 2

2

您必须在递归到子目录之前读取(并缓存)整个列表,否则顶级请求将在您完成子目录列表之前超时。

您可以继续使用ReadLine,无需自己使用ReadToEnd和分隔线。

void ListFtpDirectory(
    string url, string rootPath, NetworkCredential credentials, List<string> list)
{
    FtpWebRequest listRequest = (FtpWebRequest)WebRequest.Create(url + rootPath);
    listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
    listRequest.Credentials = credentials;

    List<string> lines = new List<string>();

    using (FtpWebResponse listResponse = (FtpWebResponse)listRequest.GetResponse())
    using (Stream listStream = listResponse.GetResponseStream())
    using (StreamReader listReader = new StreamReader(listStream))
    {
        while (!listReader.EndOfStream)
        {
            lines.Add(listReader.ReadLine());
        }
    }

    foreach (string line in lines)
    {
        string[] tokens =
            line.Split(new[] { ' ' }, 9, StringSplitOptions.RemoveEmptyEntries);
        string name = tokens[8];
        string permissions = tokens[0];

        string filePath = rootPath + name;

        if (permissions[0] == 'd')
        {
            ListFtpDirectory(url, filePath + "/", credentials, list);
        }
        else
        {
            list.Add(filePath);
        }
    }
}

使用如下功能:

List<string> list = new List<string>();
NetworkCredential credentials = new NetworkCredential("user", "mypassword");
string url = "ftp://ftp.example.com/";
ListFtpDirectory(url, "", credentials, list);

上述方法的一个缺点是它必须解析特定于服务器的列表来检索有关文件和文件夹的信息。上面的代码需要一个常见的 *nix 样式列表。但是许多服务器使用不同的格式。

不幸的FtpWebRequest是不支持该MLSD命令,这是在 FTP 协议中检索具有文件属性的目录列表的唯一可移植方式。


MLSD如果您想避免解析服务器特定目录列表格式的麻烦,请使用支持命令和/或解析各种列表格式的第三方库LIST;和递归下载。

例如,使用WinSCP .NET 程序集,您可以通过一次调用列出整个目录Session.EnumerateRemoteFiles

// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Ftp,
    HostName = "ftp.example.com",
    UserName = "user",
    Password = "mypassword",
};

using (Session session = new Session())
{
    // Connect
    session.Open(sessionOptions);

    // List files
    IEnumerable<string> list =
        session.EnumerateRemoteFiles("/", null, EnumerationOptions.AllDirectories).
        Select(fileInfo => fileInfo.FullName);
}

MLSD如果服务器支持,WinSCP 在内部使用该命令。如果没有,它会使用该LIST命令并支持数十种不同的列表格式。

(我是WinSCP的作者)

于 2017-11-30T07:10:51.280 回答
1

我正在做类似的事情,但不是对每个都使用 StreamReader.ReadLine(),而是使用 StreamReader.ReadToEnd() 一次性完成。您不需要 ReadLine() 来获取目录列表。以下是我的整个代码(教程中解释了整个操作方法):

        FtpWebRequest request=(FtpWebRequest)FtpWebRequest.Create(path);
        request.Method=WebRequestMethods.Ftp.ListDirectoryDetails;
        List<ftpinfo> files=new List<ftpinfo>();

        //request.Proxy = System.Net.WebProxy.GetDefaultProxy();
        //request.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
        request.Credentials = new NetworkCredential(_username, _password);
        Stream rs=(Stream)request.GetResponse().GetResponseStream();

        OnStatusChange("CONNECTED: " + path, 0, 0);

        StreamReader sr = new StreamReader(rs);
        string strList = sr.ReadToEnd();
        string[] lines=null;

        if (strList.Contains("\r\n"))
        {
            lines=strList.Split(new string[] {"\r\n"},StringSplitOptions.None);
        }
        else if (strList.Contains("\n"))
        {
            lines=strList.Split(new string[] {"\n"},StringSplitOptions.None);
        }

        //now decode this string array

        if (lines==null || lines.Length == 0)
            return null;

        foreach(string line in lines)
        {
            if (line.Length==0)
                continue;
            //parse line
            Match m= GetMatchingRegex(line);
            if (m==null) {
                //failed
                throw new ApplicationException("Unable to parse line: " + line);
            }

            ftpinfo item=new ftpinfo();
            item.filename = m.Groups["name"].Value.Trim('\r');
            item.path = path;
            item.size = Convert.ToInt64(m.Groups["size"].Value);
            item.permission = m.Groups["permission"].Value;
            string _dir = m.Groups["dir"].Value;
            if(_dir.Length>0  && _dir != "-")
            {
                item.fileType = directoryEntryTypes.directory;
            } 
            else
            {
                item.fileType = directoryEntryTypes.file;
            }

            try
            {
                item.fileDateTime = DateTime.Parse(m.Groups["timestamp"].Value);
            }
            catch
            {
                item.fileDateTime = DateTime.MinValue; //null;
            }

            files.Add(item);
        }

        return files;
于 2013-04-09T16:14:05.010 回答