FTP server file upload, download and get

Source: Internet
Author: User
Tags ftp site

How to:download Files with FTPThis is the sample shows how to download a file from an FTP server.

Example:

usingSystem;usingSystem.IO;usingSystem.Net;usingSystem.Text;namespaceexamples.system.net{ Public classWebrequestgetexample { Public Static voidMain () {//Get the object used to communicate with the server.FtpWebRequest request = (FtpWebRequest) webrequest.create ("Ftp://www.contoso.com/test.htm"); Request. Method = WebRequestMethods.Ftp.DownloadFile;//This example assumes the FTP site uses anonymous Logon.Request. Credentials =NewNetworkCredential ("Anonymous","[email protected]"); FtpWebResponse response = (ftpwebresponse) request.                GetResponse (); Stream Responsestream = Response.            GetResponseStream (); StreamReader reader =NewStreamReader (Responsestream); Console.WriteLine (reader.            ReadToEnd ()); Console.WriteLine ("Download complete, status {0}", Response.                Statusdescription); Reader.            Close (); Response.          Close (); }    }}

How to:upload Files with FTPThis is the sample shows how to upload a file to an FTP server.

usingSystem;usingSystem.IO;usingSystem.Net;usingSystem.Text;namespaceexamples.system.net{ Public classWebrequestgetexample { Public Static voidMain () {//Get the object used to communicate with the server.FtpWebRequest request = (FtpWebRequest) webrequest.create ("Ftp://www.contoso.com/test.htm"); Request. Method = WebRequestMethods.Ftp.UploadFile;//This example assumes the FTP site uses anonymous Logon.Request. Credentials =NewNetworkCredential ("Anonymous","[email protected]");//Copy The contents of the file to the request stream.StreamReader Sourcestream =NewStreamReader ("Testfile.txt");byte[] filecontents = Encoding.UTF8.GetBytes (Sourcestream.readtoend ());            Sourcestream.close (); Request.            ContentLength = Filecontents.length; Stream requeststream = Request.            GetRequestStream ();            requestStream.Write (filecontents, 0, filecontents.length);            Requeststream.close (); FtpWebResponse response = (ftpwebresponse) request.                GetResponse (); Console.WriteLine ("Upload File complete, status {0}", Response.                Statusdescription); Response.            Close (); }        }    }}

How To:list Directory Contents with FTP

This is the sample shows how to list the directory contents of an FTP server.

Example:

usingSystem;usingSystem.IO;usingSystem.Net;usingSystem.Text;namespaceexamples.system.net{ Public classWebrequestgetexample { Public Static voidMain () {//Get the object used to communicate with the server.FtpWebRequest request = (FtpWebRequest) webrequest.create ("ftp://www.contoso.com/"); Request. Method = WebRequestMethods.Ftp.ListDirectoryDetails;//This example assumes the FTP site uses anonymous Logon.Request. Credentials =NewNetworkCredential ("Anonymous","[email protected]"); FtpWebResponse response = (ftpwebresponse) request.                GetResponse (); Stream Responsestream = Response.            GetResponseStream (); StreamReader reader =NewStreamReader (Responsestream); Console.WriteLine (reader.            ReadToEnd ()); Console.WriteLine ("Directory List complete, status {0}", Response.                Statusdescription); Reader.            Close (); Response.        Close (); }    }}

FTP server file upload, download and get

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.