Retrieving a List of Files from an FTP server in C#

前端 未结 3 923
星月不相逢
星月不相逢 2020-12-22 02:45

I\'m trying to retrieve a list of files from an FTP server, but I\'m getting some weird non-ASCII responses.

Here is the code that I am using:

 pub         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-22 03:33

    For what it's worth, the System.Net namespace has the FtpWebRequest and FtpWebResponse classes beginning in .Net 2.0.

    Here's some code I've used that writes the server's files to a local file:

    ...
    FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(address);
    ftpRequest.Credentials = new NetworkCredential(username, password);
    ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
    ftpRequest.KeepAlive = false;
    
    FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
    
    sr = new StreamReader(ftpResponse.GetResponseStream());
    sw = new StreamWriter(new FileStream(fileName, FileMode.Create));
    
    sw.WriteLine(sr.ReadToEnd());
    sw.Close();
    
    ftpResponse.Close();
    sr.Close();
    ...
    

提交回复
热议问题