views:

848

answers:

6

Hi,

I've multiple files on a ftp server.I do not know the names of these files except that they are all. xml files. How do I programmatically download these files using .Net's FtpWebRequest?

Thanks.

+2  A: 

Most likely you'll have to issue a Dir command that lists out all the files, then go through each one downloading it.

Here is some info on getting a directory listing.

http://msdn.microsoft.com/en-us/library/ms229716.aspx

John Boker
+1  A: 

You'll probably want to use an existing library like this one rather than write your own.

popester
+1  A: 

Take a look at the ListDirectory function. It's the equivalent of the NLIST command in FTP.

Mark Byers
A: 
FtpWebRequest __request = (FtpWebRequest)FtpWebRequest.Create(__requestLocation);
__request.Method = WebRequestMethods.Ftp.ListDirectory;

var __response = (FtpWebResponse)__request.GetResponse();
                        using (StreamReader __directoryList = new StreamReader(__response.GetResponseStream())) {
                            string ___line = __directoryList.ReadLine();
                            while (___line != null) {
                                if (!String.IsNullOrEmpty(___line)) { __output.Add(___line); }
                                ___line = __directoryList.ReadLine();
                            }

                            break;
                        }

Getting the target file...

FtpWebRequest __request = null;
FtpWebResponse __response = null;
byte[] __fileBuffer = null;
byte[] __outputBuffer = null;

__request = (FtpWebRequest)FtpWebRequest.Create(__requestLocation);
__request.Method = WebRequestMethods.Ftp.DownloadFile;

__response = (FtpWebResponse)__request.GetResponse();

using (MemoryStream __outputStream = new MemoryStream()) {
   using (Stream __responseStream = __response.GetResponseStream()) {
      using (BufferedStream ___outputBuffer = new BufferedStream(__responseStream)) {
         __fileBuffer = new byte[BLOCKSIZE];
         int ___readCount = __responseStream.Read(__fileBuffer, 0, BLOCKSIZE);

         while (___readCount > 0) {
            __outputStream.Write(__fileBuffer, 0, ___readCount);
            ___readCount = __responseStream.Read(__fileBuffer, 0, BLOCKSIZE);
         }

         __outputStream.Position = 0;
         __outputBuffer = new byte[__outputStream.Length];
         //Truncate Buffer to only the specified bytes. Store into output buffer
         Array.Copy(__outputStream.GetBuffer(), __outputBuffer, __outputStream.Length);

         break;
      }
   }
}

try { __response.Close(); } catch { }
__request = null;
__response = null;

return __outputBuffer;

Ripped out of some other code I have, so it probably wont compile and run directly.

GrayWizardx
Can this download multiple files from the ftp server?Thanks for ur inputs..
Ed
Call the second code in a loop with the output from the first. .NET natively doesnt have a command for MGET. You could try changing the command type from WebRequestMethods.Ftp.DownloadFile to "MGET", but I have no idea what the stream will look like.
GrayWizardx
Just out of curiosity, why the double and triple underscores?
J.Hendrix
Old habits die hard. Single underscore class scope, double local (function scope), triple inner scope (block level). A hold back from when I wrote C purely. They are still helpful when doing code reviews outside of a visual editor
GrayWizardx
A: 

Anybody knows How to open a ftp session with c# .net framework ?

Bernard Brlek
FtpWebRequest in .NET framework is not session oriented. It emulates stateless protocol protocol behaviour on the top of statefull FTP protocol. If you want to open session, do multiple tasks, then close session you probably have to use a third party library such as one discussed in the following answer http://stackoverflow.com/questions/1890886/downloading-multiple-files-from-ftp-server/2343689#2343689 .
Martin Vobr
A: 

Hi,

I don't know if the FtpWebRequest is a strict requirement. If you can use a third party component following code would accomplish your task:

// create client, connect and log in 
Ftp client = new Ftp();
client.Connect("ftp.example.org");
client.Login("username", "password");

// download all files in the current directory which matches the "*.xml" mask 
// at the server to the 'c:\data' directory 
client.GetFiles("*.xml", @"c:\data", FtpBatchTransferOptions.Default);

client.Disconnect();

The code uses Rebex FTP which can be downloaded here.

Disclaimer: I'm involved in the development of this product.

Martin Vobr