Click here to Skip to main content
15,916,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

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.
 
Share this answer
 
Comments
[no name] 31-May-13 3:06am    
Dear,
It's a only one file send article, i need to many files send to ftp.
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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900