Vaibhav Gaikwad
Microsoft .NET developer
Bla Code! Bla Code!

IIS 6 meta reading in .NET

Wednesday, February 12, 2014 10:23 AM
First I'll talk about what made me write this code. I had been asked to set MIME types on IIS 6 web-site using web.config. I read and read for couple of hours but did not reach any good article on it. During my reading I came of across couple of article which talked about how we can do it programmatically using windows script (vbs) etc...

So finally I decided to make a utility for myself and here's the idea for all...

First, you need some extra references to:



This will come from COM



Most of the things you can straight away do with System.DirectoryServices namespace, the above mentioned COM is needed for adding any MIME definition (MimeMapClass from IISOle)

The IIS 6 metabase can be explored via "IIS://localhost/W3SVC" path.
DirectoryEntry iis6Entry = new DirectoryEntry(path);

Now you can just iterate through all the child entries of the above defined entry.

foreach (DirectoryEntry child in iis6Entry.Children)
{
          if (child.SchemaClassName.Equals("IIsWebServer"))
          {
                    // this child is a web-site entry on your IIS 6 : call it as "siteEntry"
           }
 }

On IIS 6 all the web-site entries have a unique number assigned, and you virtual directory name is actually an attribute called as "ServerComment". So if you have a web-site called "homesite" it will have path some what like this "IIS://localhost/W3SVC/1164071614"

you can check that using DirectoryEntry.Path property. Now once you have access to the the web-site's directory entry, you need to get acess to "root" just by making a new DirectoryEntry.

DirectoryEntry siteRootEntry = new DirectoryEntry(siteEntry.Path + "/root");

siteRootEntry will have all the details about the web-site MIME definitions inside a Property element called as "MimeMap".

PropertyValueCollection mimeProps = siteRootEntry.Properties["MimeMap"];

Now to add you MIME definitions:

 if (mimeProps != null)
{
                //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.
                IISOle.MimeMapClass newObj = new IISOle.MimeMapClass();
                newObj.Extension = ".webm";
                newObj.MimeType = "webm/video";
                mimeProps.Add(newObj);
                siteRootEntry.CommitChanges(); // this call with actually save the changes to the web-site
}

That's it for this article. Things which I missed out to test are:
        1. Does the app-pool recycles after I edit the MIME entries
        2. What if I push garbage values in the MIME entries
        3. Exception scnenarios

I hope this becomes useful to someone :)





Feedback

# re: IIS 6 meta reading in .NET

Currently we use .NET frame work for our back end coding and it just awesome.First I get training from sirtuinresearch.com and then I start working.Thanks for this method. 11/19/2017 2:53 PM | Jack William

Post a comment