Search code examples
c#virtual-directoryfileinfodirectoryinfo

DirectoryInfo accessing a virtual folder


I'm trying to use DirectoryList on a virtual directory, to build up a list of files. However I get the error;

URI not supported

Is there an alternative to this that supports URLs? Here's my code so far.....

DirectoryInfo directoryinfo = new DirectoryInfo("http://localhost:1080/mydatafolder");
IEnumerable<FileInfo> fileList = directoryinfo.GetFiles();

As double check, I've made sure the directory browsing has been turned on, and I can surf to it using Opera.


Solution

  • If you want to get a list of files from a remote HTTP server you could use the HttpWebRequest class to post a directory listing request and parse the contents of the HTML index page returned by IIS.

    Here's a start:

    var request = (HttpWebRequest)WebRequest.Create("http://servername/directoryname/");
    var response = (HttpWebResponse)request.GetResponse();
    
    using (var reader = new StreamReader(response.GetResponseStream()))
    {
        string body = reader.ReadToEnd();
    }
    

    Related resources: