tags:

views:

38

answers:

3

hi, I have many images on remote server say images.foo.com/222 & i want to access file names of all files that resides in the folder 222 on images.foo.com/. i have tried following code but getting error "virtual path is not valid" :

imageserver = http://images.foo.com/222;
DirectoryInfo di = new DirectoryInfo(imageserver); // line giving exception
FileInfo[] rgFiles = di.GetFiles();
string simagename = ""; 
if (rgFiles.Count() > 0)
{
foreach (FileInfo fi in rgFiles)
{
//collect each filename from here
}
}

Please help thanks in advance gbaxi

A: 

You can't access a directory residing on the web with the DirectoryInfo class. Instead, use the WebRequest class to get a list from the URL and get the files from that list.

Patrick
hi ,can you please tell me how i can do it using webrequest class
@user255818: You will have to parse the response data manually from the URL using [GetResponse](http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.getresponse.aspx). An example is available in the link, when using HttpWebRequest. Get a response from your image site, and then put individual responses (don't forget to dispose them) to each of your files.
Patrick
+2  A: 

DirectoryInfo need a UNC path of type "\\fileserver\images"

A http address will not work

TT
A: 

The problem is that HTTP does not have a clear interface on how a directory listing is being displayed. There are roughly two choices:

  1. Parse the HTML retrieved through a WebRequest, but you won't get things like creation/modification time and user;

  2. Go with a different mechanism to retrieve the file details like FTP or File share.

Pieter