FTP File Upload in C#

this is a simple FTP file upload example

you can set your proxy credentials using request.Proxy;

private static void UploadFile(string dir, Uri target, string fileName, string username, string password, string finilizingDir, string startupPath, string logFileDirectoryName)
                    // Get the object used to communicate with the server.
                    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);
                    request.Proxy = null;
                    request.Method = WebRequestMethods.Ftp.UploadFile;

                    // logon.
                    request.Credentials = new NetworkCredential(username, password);

                    // Copy the contents of the file to the request stream.
                    StreamReader sourceStream = new StreamReader(dir + "\\" + fileName);
                    byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
                    request.ContentLength = fileContents.Length;

                    Stream requestStream = request.GetRequestStream();

                    requestStream.Write(fileContents, 0, fileContents.Length);

                    FtpWebResponse response = (FtpWebResponse)request.GetResponse();

                    if (response.StatusCode == FtpStatusCode.ClosingData)
                       Console.WriteLine(" --> Status Code is :" + response.StatusCode);

                     Console.WriteLine(" --> Upload File Complete With Status Description :" + response.StatusDescription);

                catch (Exception ex)
                     Console.WriteLine("*** Error Occurred while uploading file :" + fileName + " System Says :" + ex.Message + " **********END OF ERROR**********");


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s