如何使用 C# 创建 Windows 服务器以通过 FTP 传输文件?我有一个文件夹,每 x 分钟由我的 ERP 系统生成的 xml 文件提供...我需要通过 FTP 发送到我的外部服务器。我认为每 30 分钟运行一次 Windows 服务并循环浏览此文件夹中的所有文件并通过 FTP 发送就足够了。你能帮助我吗?有什么例子吗?使用 .net vs2k8 是我的开发环境。非常感谢
问问题
2485 次
2 回答
0
How to transfer the file is in the answer from Aghilas
I would create it as a Service so you can have it auto start
Rather than poll the directory you can just use
Maybe log any failed uploads
于 2012-09-18T18:21:44.317 回答
0
您可以尝试使用此代码
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://....");
request.Method = WebRequestMethods.Ftp.UploadFile;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential ("anonymous","...");
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader("testfile.txt");
byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();
于 2012-09-18T18:03:22.527 回答