tags:

views:

52

answers:

3

How do I get a directory listing for a folder on the web? I'm looking to download a group of small files from a folder on the web. I can do it easily with a a single file but I'm not sure how to do it for multiple files. If there was something similar to the code below but for a folder on the web I think I can do it.

    private void button1_Click(object sender, EventArgs e)
    {
        DirectoryInfo di = new DirectoryInfo("c:/myFolder");
        FileInfo[] rgFiles = di.GetFiles("*.*");
        foreach (FileInfo fi in rgFiles)
        {
            //Do Something with each of them
        }
    }

The folder is just one from my web site. e.g. mysite.com/files

Thanks

+3  A: 

If this is a server you own, you would have to connect to it with a unc path like new DirectoryInfo("\myserver\shared\path\on\driver")

If it is not your server, then you cannot do this, as that would be a huge security risk. Question needs more clarification as to what you're trying to do.

Jordan
I would be getting the files from my own web site. for example they could be in www.mysite.com/files.
JimDel
Ok, well the code you have should work then, you just need to get the right path. If the code above is running on the same server, you can do Server.MapPath() or use a full like like you have above.
Jordan
+3  A: 

Normal HTTP doesn't provide any mechanisms for doing this. Even if you had enabled directory browsing on the server and did not specify a default document, all that does is tell the web server to generate a file listing in HTML. You'd have to parse the HTML and it will vary from server to server.

There's other protocols that sit on top of HTTP like WebDAV that provide this kind of functionality but it's quite complex. FTP (or UNC share as Jordan mentions) is probably a simpler option if you have control over the server side.

Josh Einstein
Thanks for the info Josh. I'll just go with Zipping them up, downloading a single file, and then Unzipping them. Sounds easier.
JimDel
+1  A: 

As per the other options, if the server is not your own. Then you can't unless they've specifically provided a service to do so.

If it is your own, and part of your app's local machine, then go ahead and use the DirectoryInfo and FileInfo method you showed above. Just be aware of the security needs for the IIS (presumably?) application pool user.

If it is your own, but remote, then you could write a webservice that provides the listing of the files in nice format for you to consume on your other server/client. Obviously be aware of security.

Reddog