0

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.

Ed.
  • 1,654
  • 7
  • 20
  • 33

5 Answers5

2

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
  • 82,559
  • 17
  • 97
  • 130
1

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

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
0

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

popester
  • 1,936
  • 9
  • 13
0
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
  • 19,561
  • 2
  • 30
  • 43
  • 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 Dec 11 '09 at 22:40
  • 2
    Just out of curiosity, why the double and triple underscores? – J.Hendrix Dec 12 '09 at 16:32
  • 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 Dec 12 '09 at 19:54
-1

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
  • 5,757
  • 2
  • 37
  • 43