How can I get file from FTP (using C#)?

后端 未结 2 485
我寻月下人不归
我寻月下人不归 2021-02-06 11:42

Now I know how to copy files from one directory to another, this is really simple.

But now I need to do the same with files from FTP server. Can you give me some exampl

2条回答
  •  故里飘歌
    2021-02-06 12:26

    Take a look at How to: Download Files with FTP or downloading all files in directory ftp and c#

     // Get the object used to communicate with the server.
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
                request.Method = WebRequestMethods.Ftp.DownloadFile;
    
                // This example assumes the FTP site uses anonymous logon.
                request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");
    
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    
                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream);
                Console.WriteLine(reader.ReadToEnd());
    
                Console.WriteLine("Download Complete, status {0}", response.StatusDescription);
    
                reader.Close();
                reader.Dispose();
                response.Close();  
    

    Edit If you want to rename file on FTP Server take a look at this Stackoverflow question

提交回复
热议问题