我正在编写一个使用具有凭据的FTP服务器的程序。我正在尝试从服务器检索目录列表,但是当我到达行时:

string line = reader.ReadLine();

我得到的字符串仅包含:“不能打开” host:/lib1 “。”

如果我尝试获取另一行,则会抛出下一个异常:远程服务器返回一个错误:(550)不可用的文件(例如,找不到文件,没有访问)。

我肯定知道(使用另一个FTP应用程序),FTP服务器上存在“ LIB1”目录,而我的凭据(用户名和密码)是正确的。

这是我的代码:

 public class FTPClient
{
    public string UserName { get; set; }
    public string Password { get; set; }
    public string IpAddress { get; set; }
    public int Port { get; set; }

    public FTPClient(string _userName, string _password, string _address, int _port)
    {
        UserName = _userName;
        Password = _password;
        IpAddress = _address;
        Port = _port;
    }

    public void GetDirectoriesList(string _path)
    {           
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + 
        IpAddress + _path));
        request.UseBinary = true;
        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);

        string line = reader.ReadLine();
        while (line!=null)
        {
            ... //do something with line
            line = reader.ReadLine();
        }
        ...
        reader.Close();
        response.Close();


    }

我使用如下:

FTPClient ftpClient = new FTPClient("user1", "pass1", "192.168.2.110", 21);

        string dirList = ftpClient.GetDirectoriesList("/lib1");

谁能发现问题?

没有正确的解决方案

其他提示

我的解决方案:

public string[] GetDirectory()
{
    StringBuilder result = new StringBuilder();
    FtpWebRequest requestDir = (FtpWebRequest)WebRequest.Create("ftp://urserverip/");
    requestDir.Method = WebRequestMethods.Ftp.ListDirectory;
    requestDir.Credentials = new NetworkCredential("username", "password");
    FtpWebResponse responseDir = (FtpWebResponse)requestDir.GetResponse();
    StreamReader readerDir  = new StreamReader(responseDir.GetResponseStream());

    string line = readerDir.ReadLine();
    while (line != null)
    {
        result.Append(line);
        result.Append("\n");
        line = readerDir.ReadLine();
    }

    result.Remove(result.ToString().LastIndexOf('\n'), 1);
    responseDir.Close(); 
    return result.ToString().Split('\n');
}

阿卜杜勒·瓦希德(Abdul Waheed)的一些改进 回答:

  • 添加 using 块清理 FtpWebResponseStreamReader 对象;
  • 减少的弦乐操作:

    private static string[] GetDirectoryListing()
    {
        FtpWebRequest directoryListRequest = (FtpWebRequest)WebRequest.Create("ftp://urserverip/");
        directoryListRequest.Method = WebRequestMethods.Ftp.ListDirectory;
        directoryListRequest.Credentials = new NetworkCredential("username", "password");
    
        using (FtpWebResponse directoryListResponse = (FtpWebResponse)directoryListRequest.GetResponse())
        {
            using (StreamReader directoryListResponseReader = new StreamReader(directoryListResponse.GetResponseStream()))
            {
                string responseString = directoryListResponseReader.ReadToEnd();
                string[] results = responseString.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
                return results;
            }
        }
    }
    
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top