Updated on Kisan Patel
To upload files using the File Transfer Protocol (FTP), you need to use the System.Net.FtpWebRequest
class.
string uploadFilePath = @"C:\example.html"; string targetFileName = "example.pdf"; Uri ftpSite = new Uri("ftp://example.com/" + targetFileName); FileInfo fileInfo = new FileInfo(uploadFilePath); FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpSite); request.Method = WebRequestMethods.Ftp.UploadFile; request.UseBinary = true; request.ContentLength = fileInfo.Length; request.Credentials = new NetworkCredential("username", "password"); byte[] byteBuffer = new byte[4096]; using (Stream requestStream = request.GetRequestStream()) { using (FileStream fileStream = new FileStream(uploadFilePath, FileMode.Open)) { int bytesRead = 0; do { bytesRead = fileStream.Read(byteBuffer, 0, byteBuffer.Length); if (bytesRead > 0) { requestStream.Write(byteBuffer, 0, bytesRead); } } while (bytesRead > 0); } } using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) { Console.WriteLine(response.StatusDescription); }