Upload a File Using File Transfer Protocol (FTP)

There are a few things you need to consider during implementation. I've also attached the code segment to understand things better and in a more convenient way for all geeks, who want to implement it.
You need to add a reference of System.Net to use the FtpWebRequest object.
The code is given below to upload the file using FTP.
  1. string PureFileName = new FileInfo("TotalAmount").Name;  
  2. String uploadUrl = String.Format("{0}/{1}/{2}""ftp://ftp.sachinkalia.com""sachin_folder", PureFileName);  
  3. FtpWebRequest request = (FtpWebRequest) WebRequest.Create(uploadUrl);  
  4. request.Method = WebRequestMethods.Ftp.UploadFile;  
  5. // This example assumes the FTP site uses anonymous logon.  
  6. request.Credentials = new NetworkCredential("username""password");  
  7. request.Proxy = null;  
  8. request.KeepAlive = true;  
  9. request.UseBinary = true;  
  10. request.Method = WebRequestMethods.Ftp.UploadFile;  
  12. // Copy the contents of the file to the request stream.  
  13. StreamReader sourceStream = new StreamReader(@ "D:\Sapmle applications\TotalAmount.txt");  
  14. byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());  
  15. sourceStream.Close();  
  16. request.ContentLength = fileContents.Length;  
  17. Stream requestStream = request.GetRequestStream();  
  18. requestStream.Write(fileContents, 0, fileContents.Length);  
  19. requestStream.Close();  
  20. FtpWebResponse response = (FtpWebResponse) request.GetResponse();  
  21. Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription); 
The status description given above describes that the data (File) has been uploaded to the FTP site with the "Transfer OK" message.
A similar approach is for deleting any file from FTP, whilst the only difference you are required to set is WebRequestMethods.Ftp.DeleteFile. The code is available below.