Setting MIME Type Properties Using System.DirectoryServices

IIS 6.0

Set MIME mappings in IIS to specify the file name extensions that are accepted by the IIS server. MIME mappings take the form of <extension>,<MIME type> and are stored as a list of mappings in the MimeMap metabase property.

The following example shows you how to use the C# programming language to add a MIME mapping for Help files to the default Web site.

This example requires a reference to the Active DS IIS Namespace Provider in Visual Studio .NET. This reference enables you to use the IISOle namespace to access the IISMimeType class.

This example also requires Windows XP Professional Service Pack 2 or Windows Server 2003 Service Pack 1.


System.DirectoryServices can be used to get and set String and DWORD properties in the IIS metabase, and invoke most methods. However, you cannot set MIME type properties unless you are using Windows XP Professional with Service Pack 2 or Windows Server 2003 with Service Pack 1.

To keep this code example concise, it does not include code access security (CAS) parameters or parameter checking. For more information, see Code Access Security and Validating User Input to Avoid Attacks. Additionally, you can instantiate your System.DirectoryServices.DirectoryEntry object with an authentication parameter.

using System;
using System.IO;
using System.DirectoryServices;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Collections;

namespace System_DirectoryServices_DirectoryEntry_ConfigIIS
  class Program
    static void Main(string[] args)
        SetMimeTypeProperty("IIS://localhost/W3SVC/1/Root", ".hlp", "application/winhlp");

    static void SetMimeTypeProperty(string metabasePath, string newExtension, string newMimeType)
      //  metabasePath is of the form "IIS://<servername>/<path>"
      //    for example "IIS://localhost/W3SVC/1/Root" 
      //  newExtension is of the form ".<extension>", for example, ".hlp"
      //  newMimeType is of the form "<application>", for example, "application/winhlp"
      Console.WriteLine("\nAdding {1}->{2} to the MimeMap property at {0}:", metabasePath, newExtension, newMimeType);

          DirectoryEntry path = new DirectoryEntry(metabasePath);
          PropertyValueCollection propValues = path.Properties["MimeMap"];
          Console.WriteLine(" Old value of MimeMap has {0} elements", propValues.Count);

          object exists = null;
          foreach (object value in propValues)
              // IISOle requires a reference to the Active DS IIS Namespace Provider in Visual Studio .NET
              IISOle.IISMimeType mimetypeObj = (IISOle.IISMimeType)value;
              Console.WriteLine("  {0}->{1}", mimetypeObj.Extension, mimetypeObj.MimeType);
              if (newExtension == mimetypeObj.Extension)
                  exists = value;

          if (null != exists)
              Console.WriteLine(" Found an entry for {0}; removing it before adding the new one.", newExtension);

          IISOle.MimeMapClass newObj = new IISOle.MimeMapClass();
          newObj.Extension = newExtension;
          newObj.MimeType = newMimeType;
          Console.WriteLine(" Done.");

      catch (Exception ex)
          if ("HRESULT 0x80005006" == ex.Message)
              Console.WriteLine(" Property MimeMap does not exist at {0}", metabasePath);
              Console.WriteLine("Failed in SetMimeTypeProperty with the following exception: \n{0}", ex.Message);