如何使用c#.net windows服务将许多文本文件从本地计算机上传到ftp? [英] How to upload many text files from local computer to ftp using c# .net windows service?
本文介绍了如何使用c#.net windows服务将许多文本文件从本地计算机上传到ftp?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
我想使用c#.net windows服务从一个文件夹到ftp开发许多文本文件的Windows服务。
如何通过c#.net windows服务,在ftp上一次上传至少五六个文件?
请帮帮我。
先谢谢。
Ankit Agarwal
软件工程师
Hello,
I want to develop windows service for many text files from one folder to ftp using c# .net windows service.
How it can be possible through c# .net windows service,at least five or six files should be upload at a time on ftp?
Please help me.
Thanks in Advance.
Ankit Agarwal
Software Engineer
推荐答案
您可以使用.Net Framework FtpWebRequest进行FTP。请参考以下链接。
http://www.techrepublic.com/blog/howdoi/how-do-i-use-c-to-upload- and-download-files-from-an-ftp-server / 165 [ ^ ]
除此之外,如果您有多线程经验,那么您可以使用多线程同时为多个文件执行FTP。
You can use .Net Framework FtpWebRequest for FTP. Take reference from below link.
http://www.techrepublic.com/blog/howdoi/how-do-i-use-c-to-upload-and-download-files-from-an-ftp-server/165[^]
Apart from that if you have experience in multi-threading, then you can use multi-threading for doing FTP more than one file at a same time.
您好,>
使用此代码:
Hi,
use this code :
private void button1_Click(object sender, EventArgs e)
{
List fileInfo= // Store list of files to be uploaded.
Parallel.ForEach(fileInfo, objFile =>
{
UploadFile(objFile);
});
}
private bool UploadFile(FileInfo fileInfo)
{
FtpWebRequest request = null;
try
{
string ftpPath = "ftp://www.tt.com/" + fileInfo.Name
request = (FtpWebRequest)WebRequest.Create(ftpPath);
request.Credentials = new NetworkCredential("ftptest", "ftptest"); // username , password
request.Method = WebRequestMethods.Ftp.UploadFile;
request.KeepAlive = false;
request.Timeout = 60000; // 1 minute time out
request.ServicePoint.ConnectionLimit = 15; // Connection limit 15 with the ftp., By default 2, -1 means infinite.
byte[] buffer = new byte[1024];
using (FileStream fs = new FileStream(fileInfo.FullPath, FileMode.Open))
{
int dataLength = (int)fs.Length;
int bytesRead = 0;
int bytesDownloaded = 0;
using (Stream requestStream = request.GetRequestStream())
{
while (bytesRead < dataLength)
{
bytesDownloaded = fs.Read(buffer, 0, buffer.Length);
bytesRead = bytesRead + bytesDownloaded;
requestStream.Write(buffer, 0, bytesDownloaded);
}
requestStream.Close();
}
}
return true;
}
catch (Exception ex)
{
throw ex;
}
finally
{
request = null;
}
return false;
}// UploadFile
这篇关于如何使用c#.net windows服务将许多文本文件从本地计算机上传到ftp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文