Can do a class:
1 #regionFtp2 3 stringFtpserverip;//the address of the FTP server4 stringFtpremotepath;//FTP directory5 stringFtpuserid;//FTP User ID6 stringFTPPassword;//ftp Password7 stringFtpuri;//FTP URI8 9 /// <summary>Ten ///Connect to FTP One /// </summary> A /// <param name= "Ftpserverip" >FTP connection Address</param> - /// <param name= "Ftpremotepath" >Specifies the current directory after the FTP connection succeeds and defaults to the root directory if not specified</param> - /// <param name= "Ftpuserid" >User name</param> the /// <param name= "FTPPassword" >Password</param> - Public voidFtpinitial (stringFtpserverip,stringFtpremotepath,stringFtpuserid,stringFTPPassword) - { -Ftpserverip =Ftpserverip; +Ftpremotepath =Ftpremotepath; -Ftpuserid =Ftpuserid; +FTPPassword =FTPPassword; AFtpuri ="ftp://"+ Ftpserverip +"/"+ Ftpremotepath +"/"; at } - - /// <summary> - ///Connect to FTP - /// </summary> - /// <param name= "Ftpremotepath" >Specifies the current directory after the FTP connection succeeds and defaults to the root directory if not specified</param> in Public voidFtpinitial (stringFtpremotepath) - { toFtpserverip ="10.0.0.1"; +Ftpremotepath =Ftpremotepath; -Ftpuserid ="ftp_user_id"; theFTPPassword ="pwd"; *Ftpuri ="ftp://"+ Ftpserverip +"/"+ Ftpremotepath +"/"; $ }Panax Notoginseng - /// <summary> the ///Uploading using FTP + /// </summary> A /// <param name= "filename" >file name of the upload</param> the Public voidFtpupload (stringfilename) + { -FileInfo Fileinf =NewFileInfo (filename); $ stringURI = Ftpuri +Fileinf.name; $ ftpwebrequest reqftp; - -Reqftp = (ftpwebrequest) ftpwebrequest.create (NewUri (URI));//to create a FtpWebRequest object from a URI theReqftp.credentials =NewNetworkCredential (Ftpuserid, FTPPassword); -Reqftp.keepalive =false;WuyiReqftp.method =WebRequestMethods.Ftp.UploadFile; theReqftp.usebinary =true; -Reqftp.contentlength =fileinf.length; Wu intBuffLength =2048; - byte[] Buff =New byte[bufflength]; About intContentlen; $FileStream fs =Fileinf.openread (); - Try - { -Stream STRM =Reqftp.getrequeststream (); AContentlen = fs. Read (Buff,0, bufflength); + while(Contentlen! =0) the { -Strm. Write (Buff,0, Contentlen); $Contentlen = fs. Read (Buff,0, bufflength); the } the STRM. Close (); the FS. Close (); the } - Catch(Exception ex) in { the Console.WriteLine (ex. Message); the } About } the the #endregion
Asp. NET, using FTP