JimDel
JimDel

Reputation: 4359

How do i get a directory listing for a folder on the web?

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

Upvotes: 1

Views: 915

Answers (3)

Reddog
Reddog

Reputation: 15579

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.

Upvotes: 1

Josh
Josh

Reputation: 69272

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.

Upvotes: 4

Jordan
Jordan

Reputation: 32552

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.

Upvotes: 3

Related Questions