Public class ftphelper {// <summary> // FTP upload // </Summary> Public static int uploadftp (string filepath, string filename, string ftpserverip, string ftpuserid, string ftppassword) {fileinfo fileinf = new fileinfo (filepath + "\" + filename); string uri = "ftp: //" + ftpserverip + "/" + fileinf. name; ftpwebrequest reqftp; // create ftpwebrequest object from the URI provided reqftp = (ftpwebreq Uest) ftpwebrequest. create (New uri ("ftp: //" + ftpserverip + "/" + fileinf. name); try {// provide the webpermission credintials reqftp. credentials = new networkcredential (ftpuserid, ftppassword); // by default keepalive is true, where the control connection is not closed // after a command is executed. reqftp. keepalive = false; // specify the command to be executed. reqftp. method = webrequestmetho DS. FTP. uploadfile; // specify the data transfer type. reqftp. usebinary = true; // configure y the server about the size of the uploaded file reqftp. contentlength = fileinf. length; // the buffer size is set to 2kb int bufflength = 2048; byte [] buff = new byte [bufflength]; int contentlen; // opens a file stream (system. io. filestream) to read the file to be uploaded // filestream FS = fileinf. openread (); f Ilestream FS = fileinf. open (filemode. open, fileaccess. read, fileshare. readwrite); // stream to which the file to be upload is written stream STRM = reqftp. getrequeststream (); // read from the file stream 2kb at a time contentlen = FS. read (buff, 0, bufflength); // till stream content ends while (contentlen! = 0) {// write content from the file stream to the FTP upload stream STRM. write (buff, 0, contentlen); contentlen = FS. read (buff, 0, bufflength);} // close the file stream and the request stream STRM. close (); FS. close (); Return 0;} catch (exception ex) {reqftp. abort (); // logging. writeerror (ex. message + ex. stacktrace); Return-2 ;}/// <summary> // download through FTP /// </Summary> Public static int downloadftp (string filepath, string filename, string ftpserverip, string ftpuserid, string ftppassword) {ftpwebrequest reqftp; try {// filepath = <the full path where the file is to be created.>, // filename = <name of the file to be created (need not be the name of the file on FTP Server).> filestream outputstream = new filestream (filepath + "\" + filename, filemode. create); reqftp = (ftpwebrequest) ftpwebrequest. create (New uri ("ftp: //" + ftpserverip + "/" + filename); reqftp. method = webrequestmethods. FTP. downloadfile; reqftp. usebinary = true; reqftp. keepalive = false; reqftp. credentials = new networkcredential (ftpuserid, ftppassword); ftpwebresponse response = (ftpwebresponse) reqftp. getresponse (); stream ftpstream = response. getresponsestream (); long Cl = response. contentlength; int buffersize = 2048; int readcount; byte [] buffer = new byte [buffersize]; readcount = ftpstream. read (buffer, 0, buffersize); While (readcount> 0) {outputstream. write (buffer, 0, readcount); readcount = ftpstream. read (buffer, 0, buffersize);} ftpstream. close (); outputstream. close (); response. close (); Return 0;} catch (exception ex) {// logging. writeerror (ex. message + ex. stacktrace); // system. windows. forms. messageBox. show (ex. message); Return-2 ;}}}
[C #] tool-FTP upload/download