//First create FtpWebRequest object with the ftpURL
System.Net.FtpWebRequest ftpRequest = (System.Net.FtpWebRequest)System.Net.WebRequest.Create(CompleteFTPPath);
//Set the ftp Method, like 'UploadFile' for upload
ftpRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
//Give your user name & password of the ftp login
ftpRequest.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings.Get("username"), ConfigurationManager.AppSettings.Get("password"));
ftpRequest.UsePassive = false;
// By default KeepAlive is true, where the control connection is not closed
// after a command is executed.
ftpRequest.KeepAlive = false;
// Specify the data transfer type.
ftpRequest.UseBinary = true;
// Opens a file stream (System.IO.FileStream) to read the file to be uploaded
System.IO.FileStream streamObj = System.IO.File.OpenRead(CompleteLocalPath);
// Notify the server about the size of the uploaded file
ftpRequest.ContentLength = streamObj.Length;
// The buffer size is set to 2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
// Stream to which the file to be upload is written
System.IO.Stream writeStream = ftpRequest.GetRequestStream();
// Read from the file stream 2kb at a time
contentLen = streamObj.Read(buff, 0, buffLength);
// Till Stream content ends
while (contentLen != 0)
{
// Write Content from the file stream to the FTP Upload Stream
writeStream.Write(buff, 0, contentLen);
contentLen = streamObj.Read(buff, 0, buffLength);
}
// Close the file stream and the Request Stream
streamObj.Close();
writeStream.Close();