我如何从FTP获取文件(使用C#)? [英] How can I get file from FTP (using C#)?
本文介绍了我如何从FTP获取文件(使用C#)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
现在我知道如何将文件从一个目录复制到另一个目录,这非常简单。
但现在我需要对来自FTP服务器的文件执行相同的操作。你可以给我一些例子,如何从FTP更改文件名时获取文件吗?
http://msdn.microsoft.com/en-us/library/ms229711.aspx =nofollow noreferrer>如何:使用FTP下载文件或下载目录ftp和c#中的所有文件 //获取用于与服务器通信的对象。
FtpWebRequest请求=(FtpWebRequest)WebRequest.Create(ftp://www.contoso.com/test.htm);
request.Method = WebRequestMethods.Ftp.DownloadFile;
//此示例假定FTP站点使用匿名登录。
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(下载完成,状态{0},response.StatusDescription);
reader.Close();
reader.Dispose();
response.Close();
编辑
如果要在FTP服务器上重命名文件看看这个 Stackoverflow问题
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 example how to get file from FTP while changing its name?
解决方案
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
这篇关于我如何从FTP获取文件(使用C#)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文