Using .NET 2.0, how do I FTP to the server, get the file and delete the file?

Does .NET (C #) have built-in libraries for FTP? I don't need anything crazy ... very simple.

I need:

  • FTP to account
  • Detect if connection was rejected
  • Get text file
  • Delete text file

What is the easiest way to do this?

+6
c # ftp
source share
5 answers

Use the FtpWebRequest class or the plain old WebClient .

FTP to your account and get the file:

WebClient request = new WebClient(); request.Credentials = new NetworkCredential("anonymous", " janeDoe@contoso.com "); try { // serverUri here uses the FTP scheme ("ftp://"). byte[] newFileData = request.DownloadData(serverUri.ToString()); string fileString = Encoding.UTF8.GetString(newFileData); } catch (WebException ex) { // Detect and handle login failures etc here } 

Delete a file:

 FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri); request.Method = WebRequestMethods.Ftp.DeleteFile; FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Console.WriteLine("Delete status: {0}", response.StatusDescription); response.Close(); 

(Code samples from MSDN.)

+7
source share

This article implements a graphical interface for an FTP client using .NET 2.0 and has full source code with examples.

Sample code includes connecting, downloading and downloading, as well as good comments and clarifications.

+2
source share

Just use the FtpWebRequest class. It already processes everything you need.

+2
source share
+1
source share

Use edtFTPnet , the free, open source .NET FTP library that will do everything you need.

0
source share

All Articles