using System;
using System.Collections.Specialized;
using System.DirectoryServices;
using System.Reflection;

namespace Sex
{
    public partial class Sex : System.Web.UI.Page
    {
        public static class Mime
        {
            static public NameValueCollection mimemap = new NameValueCollection();
            public static string GetMimeType(string Extension)
            {
                if (mimemap.Count == 0) BuildMimeMap();
                return mimemap[Extension];
            }
            public static void BuildMimeMap()
            {
                using (DirectoryEntry entry = new DirectoryEntry("IIS://localhost/MimeMap"))
                {
                    PropertyValueCollection properties;
                    try
                    {
                        properties = entry.Properties["MimeMap"];
                    }
                    catch
                    {
                        mimemap.Add("Error", "Mime Types cannot be loaded");
                        return;
                    }
                    Type type = properties[0].GetType();

                    foreach (object property in properties)
                    {
                        BindingFlags f = BindingFlags.GetProperty;
                        string ext = type.InvokeMember("Extension", f, null, property, null) as String;
                        string mime = type.InvokeMember("MimeType", f, null, property, null) as String;
                        mimemap.Add(ext, mime);
                    }
                }
            }
        }
    }
}