Browsing Web Directories using C#

To allow directory browsing in IIS, you need to navigate to the web site in IIS Manager, click the Browse Directory icon, and click the Activate button to the right.

Directory browsing generates an HTML view on the file system. Unfortunately, the HTML is not standardized and may different between different versions of IIS, and other web servers.

In case of IIS 7.5, the typical output looks like this:

<A HREF="/path/">[To Parent Directory]</A><br>
18.09.2011 13:44 25266 <A HREF="/path/images/foo.jpg">foo.jpg</A><br>
18.09.2011 13:44 &lt;dir&gt; <A HREF="/path/images/subdir/">subdir</A><br>
  • The first line links to the parent directory.
  • The seconds line is a file contained in the requested directory.
  • The third line is a subdirectory below the requested directory.

To parse the files and directories, we need two separate regular expressions:


This solution is based on an answer on SO, but with the following changes

  • added the distinction between files and directories
  • fixed the regex to non-greedy
  • added “name” and “url” group names
var request = (HttpWebRequest)WebRequest.Create(url);
using (var response = (HttpWebResponse)request.GetResponse())
  using (var reader = new StreamReader(response.GetResponseStream()))
    string html = reader.ReadToEnd();

    var rexDir = new Regex(
    var rexFile = new Regex(

    var matchedDirs = rexDir.Matches(html);
    if (matchedDirs.Count > 0)
      foreach (Match m in matchedDirs)
        Console.WriteLine(m.Groups["name"] + ": " + m.Groups["url"]);

    var matchedFiles = rexFile.Matches(html);
    if (matchedFiles.Count > 0)
      foreach (Match m in matchedFiles)
        Console.WriteLine(m.Groups["name"] + ": " + m.Groups["url"]);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: