0

I want to get all files names and directory list from one web directory through http:// using C#.

For example:

Directory.GetFiles(folder)**

is used to get files list from local directory, same as i want to get files list from web directory.

abatishchev
  • 98,240
  • 88
  • 296
  • 433

4 Answers4

5
System.IO.Directory.GetFiles(HttpContext.Current.Server.MapPath("~/PortalPage/"))
Sirko
  • 72,589
  • 19
  • 149
  • 183
massi
  • 97
  • 4
4

Well, I don't believe HTTP has a "directory index" you can directly use.

You can't pass url to Directory.GetFiles method, this method takes as a parameter physical path of url (if it is accessible of course).

Check out Server.MapPath method instead.

The MapPath method maps the specified relative or virtual path to the corresponding physical directory on the server.

Path

Specifies the relative or virtual path to map to a physical directory. If Path starts with either a forward (/) or backward slash (), the MapPath method returns a path as if Path were a full, virtual path. If Path doesn't start with a slash, the MapPath method returns a path relative to the directory of the .asp file being processed.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • 1
    +1: side note - `MapPath` going to help only if you are interested in enumerating files on your own server... otherwise not possible (and generally there is no "files" behind most of urls). [Sitemap](http://en.wikipedia.org/wiki/Sitemaps) may give some insight if supported by particular server... – Alexei Levenkov Apr 12 '13 at 07:16
1

HTTP in itself does NOT have any function to list the FILE content in a folder. This is impossible, for one thing, as the HTTP specification does not require a URL to be mapped TO a folder. The whole basis of programmable webpages is based on a URL NOT being mapped to a STATIC file.

However. There ARE many extensions to the HTTP protocol. WebDAV IS a protocol for mapping an HTTP URI to a directory in some location. Check out here for how to query a WebDAV service for the content of a folder.

However it can't do recursive queries as far as I know.

Community
  • 1
  • 1
Aron
  • 15,464
  • 3
  • 31
  • 64
0

You can't use Directory.GetFiles on a URL.

Consider the example:

string baseURL = "http://download.example.org/export/dump/";
WebClient client = new WebClient();            
string content = client.DownloadString(baseURL);

Then you run a loop inside.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396