Look up the default web in IIS

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;
        }

Couple great tools for JavaScript and regex…

If you are whipping up some JavaScript and want a great place to build/run and perhaps share with other, then you need to check out JSFiddle.  This tool is fantastic, and a great way to collaboratively work on JavaScript.  For debugging and/or writing out to a console, consider using Firefox’s [awesome] firebug addin (IE Developer tools works pretty well too).

For times when you need to use regular expressions – and you’re either not strong at them, or have been away from them for more than 2 minutes (the time it takes to forget most of the syntax), then check out RegExPal.  This little utility is handy, and give great real-time visual indications of matches.