Other Posts in IO

  1. Uploading and Downloading a File to an FTP Server Using C#
  2. Deserializing/Serializing SOAP Messages in C#

Uploading and Downloading a File to an FTP Server Using C#

2/9/2009

I've never been a big fan of FTP servers (or IRC, or really anything where the average/popular tool is a pain to deal with).  That being said, I've had to use it quite a bit considering what I do for a living. To be honest though, I don't like the tools that are out there for dealing with it. So as always I decided to roll my own. I'm not going to give out all of the code, but I figured it would be nice to show how to download, as well as upload, a file to an FTP server:

   1: public static string GetFileContents(Uri FileName, string UserName, string Password)
   2: {
   3:     WebClient Client = null;
   4:     StreamReader Reader = null;
   5:     try
   6:     {
   7:         Client = new WebClient();
   8:         Client.Credentials = new NetworkCredential(UserName, Password);
   9:         Reader = new StreamReader(Client.OpenRead(FileName));
  10:         string Contents = Reader.ReadToEnd();
  11:         Reader.Close();
  12:         return Contents;
  13:     }
  14:     catch
  15:     {
  16:         return "";
  17:     }
  18:     finally
  19:     {
  20:         if (Reader != null)
  21:         {
  22:             Reader.Close();
  23:         }
  24:     }
  25: } 
  26:  
  27:   
  28:  
  29:  public static void SaveFile(string Content, string FileName, Uri FTPServer, string UserName, string Password)
  30: {
  31:     try
  32:     {
  33:         Uri TempURI = new Uri(Path.Combine(FTPServer.ToString(), FileName));
  34:         FtpWebRequest FTPRequest = (FtpWebRequest)FtpWebRequest.Create(TempURI);
  35:         FTPRequest.Credentials = new NetworkCredential(UserName, Password);
  36:         FTPRequest.KeepAlive = false;
  37:         FTPRequest.Method = WebRequestMethods.Ftp.UploadFile;
  38:         FTPRequest.UseBinary = true;
  39:         FTPRequest.ContentLength = Content.Length;
  40:         FTPRequest.Proxy = null;
  41:         using (Stream TempStream = FTPRequest.GetRequestStream())
  42:         {
  43:             System.Text.ASCIIEncoding TempEncoding = new System.Text.ASCIIEncoding();
  44:             byte[] TempBytes = TempEncoding.GetBytes(Content);
  45:             TempStream.Write(TempBytes, 0, TempBytes.Length);
  46:         }
  47:         FTPRequest.GetResponse();
  48:     }
  49:     catch { }
  50: }

Those two functions are all that is needed to accomplish what you want. GetFileContents downloads the file (although it only works for text files, it can easily be modified for binary). As input it takes in the location of the file that you want to download (which should include the ftp://servername/ portion), the user name needed for connecting to the server, and the password. Note that if it allows anonymous access, leaving those blank will work. The SaveFile function on the other hand will upload the file to an FTP server. That function needs a bit more information, the content that you wish to put in the file, the file name (which should include the directory information), the server name (once again, use the ftp://servername/), and once again the user name and password. You could modify the code to simply open and copy the file, I just didn't have the need at that level. Anyway, that's all there is to it and I hope this helps you out. So try it out, leave feedback, and happy coding.



Comments

Joemarie Amparo
November 25, 2010 9:15 PM

Hello James,It's okay. Anyways, I tried another way of looking for files on the server and it's good enough. I can't still display the files though, still fine. Thanks for your help James.

James Craig
November 23, 2010 1:20 PM

Sorry for the delayed response. I don't have any code to display the file listing, creation date, etc. I'd look into something like this: http://www.codeproject.com/KB/IP/FtpClient.aspx

joemarie amparo
November 19, 2010 3:31 AM

hello James,Thanks for the tip. My problem is there are many files in the server (fyi: files will appear when "dir" is typed using the CMD). The basis of downloading the file is the date of creation. How would I locate the latest file created on the server?Also, I wanted to display the files in the ftp server, but I still can't get the right code to do that. I am using asp.net mvc C#.Please help.Thanks.

James Craig
November 18, 2010 9:32 AM

Well you would have the location of the ftp server, the user name, and password for the ftp server. All you would need to do is create a WebClient object, set the Credentials property, and then call DownloadFile:using(WebClient Client=new WebClient()){Client.Credentials=new NetworkCredential(UserName,Password);Client.DownloadFile(FileLocationOnFTPServer,LocationYouWantToSaveTheFileTo);}That's all you would need (filling in the appropriate fields as needed).

joemarie amparo
November 17, 2010 9:22 PM

Hello James,I am also trying to build a program that is somewhat the same like this. I need to download the latest file from the ftp server and save it in my local drive. I am new to this and to programming world. I need your guidance. thanks.Thank you very much,joemarieamparo@yahoo.com