October 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Access Newly Available Network Information with .NET 2.0

  • January 21, 2005
  • By Mark Strawmyer
  • Send Email »
  • More Articles »

Using File Transfer Protocol (FTP)

One of the features missing from the BCL has been the ability to support FTP requests. This has been rectified with the addition of the FtpWebRequest object, which provides a managed interface for issuing FTP commands. It is included in System.Net namespace. The Create method of the WebRequest class is used to retrieve an instance of FtpWebRequest, which can then be used for various FTP operations.

FTP sample code

The following sample code connects to the local machine via FTP and downloads a specific file. This assumes you have FTP running on your local machine and a text file exists with the particular name. The FtpWebRequest has a Method property on it that is used to specify the action to take. By default, the action is FtpMethods.DownloadFile, so it is not necessary to set the property value. The code assumes you copy the TestFtp method into the example class and add a call to TestFtp in the Main method:

public static void TestFtp(){   // Set up the FTP connection   FtpWebRequest ftpRequest =      (FtpWebRequest)WebRequest.Create("ftp://localhost/testfile.txt");   ftpRequest.Credentials = new      NetworkCredential("anonymous", "mstrawmyer@crowechizek.com");   // Initiate the request and read the response   FtpWebResponse ftpResponse =      (FtpWebResponse)ftpRequest.GetResponse();   Stream ftpResponseStream = ftpResponse.GetResponseStream();   StreamReader reader = new StreamReader(ftpResponseStream,                                          System.Text.Encoding.UTF8);   // Display the file contents   Console.WriteLine(reader.ReadToEnd());   Console.ReadLine();}

Simplified FTP sample code

There is an easier way to perform downloads from an FTP site using the WebClient object. The following sample code demonstrates using it to download the same file accessed in the prior example. As you can see, it is definitely a simplified approach. The code assumes you copy the TestSimpleFtp method into the example class and add a call to TestSimpleFtp in the Main method:

public static void TestSimpleFtp(){   WebClient client = new WebClient();   client.Credentials = new NetworkCredential("anonymous",                                              "mstrawmyer@crowechizek.com");   client.DownloadFile("ftp://localhost/testfile.txt", "C:\\Demofile.txt");}

Future Columns

The topic of the next column has yet to be determined. If you have something in particular that you would like to see explained, please e-mail me at mstrawmyer@crowechizek.com.

About the Author

Mark Strawmyer (MCSD, MCSE, MCDBA) is a senior architect of .NET applications for large and mid-sized organizations. Mark is a technology leader with Crowe Chizek in Indianapolis, Indiana. He specializes in architecture, design, and development of Microsoft-based solutions. Mark was honored to be named a Microsoft MVP for application development with C#. You can reach Mark at mstrawmyer@crowechizek.com.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel