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