upload file to ftp server using c#

Import below Namespaces
using System.IO;
using System.Net;
Create a Procedure as shown below
private static void UploadFileToFTP(string source)
    {
        string _ftpurl = "some ftp url"; // like "ftp://10.0.0.10/folder1/folder2";
        string _ftpusername = "your ftp username"; //like "admin";
        string _ftppassword = "your ftp password"; //like "pass@123";
        try
        {
            string filename = Path.GetFileName(source);
            string ftpfullpath = _ftpurl + @"/" + filename;
            FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
            ftp.Credentials = new NetworkCredential(_ftpusername, _ftppassword);

            ftp.KeepAlive = true;
            ftp.UseBinary = true;
            ftp.Method = WebRequestMethods.Ftp.UploadFile;
            FileStream fs = File.OpenRead(source);
            byte[] buffer = new byte[fs.Length];
            fs.Read(buffer, 0, buffer.Length);
            fs.Close();
            Stream ftpstream = ftp.GetRequestStream();
            ftpstream.Write(buffer, 0, buffer.Length);
            ftpstream.Close();
        }
        catch (Exception ex)
        {
            throw;
        }
    }
Now just call the procedure on whatever event you want as below
string _path = @"d:\text.txt";
 UploadFileToFTP(_path);

Reference: Muthukumar (http://nadarmuthukumar.blogspot.in)

1 comments:

This is a great FTP Library for C#, it is reasonably priced too:
https://www.kellermansoftware.com/p-39-net-ftp-library.aspx

Twitter Delicious Facebook Digg Stumbleupon Favorites More