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 <dir> <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:
<dir>\s*\<A\s+HREF=""(?<url>.+?)""\>(?<name>.+?)\</A\> \d+\s*\<A\s+HREF=""(?<url>.+?)""\>(?<name>.+?\.(png|gif|jpg|jpeg))\</A\>
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( @"<dir>\s*\<A\s+HREF=""(?<url>.+?)""\>(?<name>.+?)\</A\>", RegexOptions.IgnoreCase); var rexFile = new Regex( @"\d+\s*\<A\s+HREF=""(?<url>.+?)""\>(?<name>.+?\.(png|gif|jpg|jpeg))\</A\>", RegexOptions.IgnoreCase); var matchedDirs = rexDir.Matches(html); if (matchedDirs.Count > 0) { Console.WriteLine("dirs"); foreach (Match m in matchedDirs) { Console.WriteLine(m.Groups["name"] + ": " + m.Groups["url"]); } } var matchedFiles = rexFile.Matches(html); if (matchedFiles.Count > 0) { Console.WriteLine("files"); foreach (Match m in matchedFiles) { Console.WriteLine(m.Groups["name"] + ": " + m.Groups["url"]); } } } }