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

Ever wish JavaScriptSerializer had a non-generic Deserialize method (like DataContractJSONSerializer)?

Well, Rex informed me of the handy MethodInfo.MakeGenericMethod which creates a constructed generic method, allowing us to avoid having to declare the generic types at design time.  So, now we can bolt on a Deserialize method (note, we cache the constructed generic methods) (formatted to appease wordpress)

public static class JavaScriptSerializerExtensions
    {
        private static object _lock = new object();
        private static IDictionary<Type, MethodInfo> _deserializers
               = new Dictionary<Type, MethodInfo>();

        private static MethodInfo GetDeserializeMethod(Type type)
        {
            lock (_lock)
            {
                if (_deserializers.ContainsKey(type)==false)
                {
                    var mi = typeof(JavaScriptSerializer)
                        .GetMethod("Deserialize");
                    _deserializers[type] = mi.MakeGenericMethod(type);
                }
            }
            return _deserializers[type];
        }

        public static object Deserialize(
               this JavaScriptSerializer serializer, Type type, string json)
        {
            return GetDeserializeMethod(type).Invoke(
                serializer, new object[] { json });
        }
    }

Simplify with Linq

Takes simple tasks and makes them so brutally clean.  Simple examples…

int [] i = { 1, 3, 5, 6,7, 9 };
int  [] i2 = { 2, 4, 5, 8, 10, 3, 5 }; 

 // Combination of all (repeats included).
IEnumerable<int> concat = i.Concat(i2).ToList();

// Only those ints from i not in i2.
IEnumerable<int> except = i.Except(i2).ToList(); 
  // Only the unique ints found in i and i2, and order them.
IEnumerable<int> union = i.Union(i2).OrderBy(r => r).ToList();
  // Only those ints that exist in both i and i2.
IEnumerable<int> intersect = i.Intersect(i2).ToList();