The FtpWebRequest class under the System.Net namespace implements the. NET implementation of the FTP protocol.
- FTPWEBREQUEST.KEEPALIVE Specifies whether the server should close the connection immediately after the request has completed
- Ftpwebrequest.usebinary specifies that files are transferred in binary mode
- Ftpwebrequest.method to set FTP commands
- WebRequestMethods.Ftp.UploadFile is a command to upload a file
When uploading a file using the FtpWebRequest object, you need to write data to the stream returned by the GetRequestStream method.
You need to refer to the following namespaces
UsingSystem. Net; UsingSystem. IO;
FTP Upload file Code implementation:
Public voidFtpfile(StringFtpfilepath, StringInputfilepath) { StringFtphost= "127.0.0.1"; Here correct hostname or IP of the FTP server to be given StringFtpfullpath= "ftp://" +Ftphost+Ftpfilepath; FtpWebRequestFtp= (FtpWebRequest)FtpWebRequest.Create(Ftpfullpath);Ftp.Credentials = New NetworkCredential("UserID", "Password"); UserID and password for the FTP server to givenFtp.KeepAlive = True;Ftp.Usebinary = True;Ftp.Method = WebRequestMethods.Ftp.UploadFile; FileStreamFs= File.OpenRead(Inputfilepath); Byte[]Buffer= New Byte[Fs.Length];Fs.Read(Buffer, 0,Bufferlength); Fs. Closestream FtpStream = Ftpgetrequeststream (); Ftpstream. Write (buffer, 0, Buffer. Length Ftpstream. Close /span>
Call Method:
Ftpfile(@"/testfolder/testfile.xml",@"C:\testfile.xml");
C # uploading files using FtpWebRequest