Go FTP operation class, downloading files from FTP

Source: Internet
Author: User

Using system;using system.collections.generic;using system.text;using system.io;using System.Net;using System.windows.forms;namespace pcmdataimporter{///<summary>//Package Two operations for ftp: Download file download () and get file list information on FTP server G        Etfilelist ()///</summary> public class Ftpoperater {private string Ftpserverip;        private string Ftpuser;        private string ftppwd;        Private Systemparareader spreader;            Public Ftpoperater () {spreader = new Systemparareader ();            This.ftpserverip = Spreader.getsysparavalue ("Ftpserver");            This.ftpuser = Spreader.getsysparavalue ("Ftpuser");        This.ftppwd = Spreader.getsysparavalue ("Ftppwd"); }///<summary>//Get the file information on the FTP server///</summary>//<returns> A string that stores all file information            Array </returns> public string[] Getfilelist () {string[] downloadfiles;         StringBuilder result = new StringBuilder ();   FtpWebRequest reqftp;                try {reqftp = (ftpwebrequest) ftpwebrequest.create (New Uri ("ftp://" + Ftpserverip + "/"));                Reqftp.usebinary = true;                Reqftp.credentials = new NetworkCredential (Ftpuser, ftppwd);                Reqftp.method = WebRequestMethods.Ftp.ListDirectory;                WebResponse response = Reqftp.getresponse (); StreamReader reader = new StreamReader (response.                GetResponseStream ()); String line = reader.                ReadLine (); while (line! = null) {result.                    Append (line); Result.                    Append ("\ n"); line = reader.                ReadLine (); } result. Remove (result. ToString ().                LastIndexOf (' \ n '), 1); Reader.                Close (); Response.                Close (); return result. ToString ().            Split (' \ n '); } catch (Exception ex) {system.windows.Forms.MessageBox.Show ("Get File information failed:" +ex.                Message, "Operation failed", Messageboxbuttons.ok,messageboxicon.error);                Downloadfiles = null;            return downloadfiles; }}///<summary>//Get the size of the specified file on FTP///</summary>//<param name= "file        Name "> File name </param>//<returns> File size </returns> public long getfilesize (string filename)            {FtpWebRequest reqftp;            Long fileSize = 0; try {reqftp = (ftpwebrequest) ftpwebrequest.create (New Uri ("ftp://" + Ftpserverip + "/" + Filena                Me));                Reqftp.method = WebRequestMethods.Ftp.GetFileSize;                Reqftp.usebinary = true;                Reqftp.credentials = new NetworkCredential (Ftpuser, ftppwd);                FtpWebResponse response = (ftpwebresponse) reqftp.getresponse (); Stream FtpStream = Response.                GetResponseStream (); FileSize = Response. ContEntlength;                Ftpstream.close (); Response.            Close (); } catch (Exception ex) {MessageBox.Show ("an exception occurred when getting file size: \ n" + ex.) Message, "Get File size failed!"            ", MessageBoxButtons.OK, Messageboxicon.error);        } return fileSize; }///<summary>//To implement FTP download operation///</summary>//<param name= "FilePath" > Save to this Name of the ground </param>//<param name= "filename" > Remote file name </param> public void Download (string filepat            H, String fileName) {FtpWebRequest reqftp;                 try {//filepath = <<the full path where the file was to be created.>>                FileName = <<name of the file to being created (need not being the Name of the 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.credentials = new NetworkCredential (Ftpuser, ftppwd);                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 ();   } catch (Exception ex) {             MessageBox.Show (ex.            Message); }        }    }}

Go FTP operation class, downloading files from FTP

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.