Default as defined by port 80, no host headers and is running on the local machine:
private string GetDefaultWebPath()
{
string result = null;
using (var de = new DirectoryEntry("IIS://Localhost/W3SVC"))
{
foreach (DirectoryEntry child in de.Children
.Cast<DirectoryEntry>()
.Where(d => d.SchemaClassName == "IIsWebServer" &&
d.Properties["ServerState"]
.Value.ToString() == "2" /* Started*/))
{
var bindings = child.Properties
.Cast<PropertyValueCollection>()
.Where(i => i.PropertyName == "ServerBindings")
.ToList().Cast<PropertyValueCollection>();
if (bindings.Count() == 1 && bindings
.First().Value.ToString() == ":80:")
{
var root = child.Children
.Cast<DirectoryEntry>()
.FirstOrDefault(d => d.SchemaClassName == "IIsWebVirtualDir");
if (root != null)
{
result = root.Properties["Path"].Value.ToString();
}
}
}
}
if (string.IsNullOrEmpty(result))
{
throw new Exception("Could not determine the default web.");
}
return result;
}