Turn: http://www.cnblogs.com/jpwar/archive/2007/03/15/675956.html
Recently, I am working on an installation program. One step is to add a new MIME type. log to IIS so that users can download IIS logs.
All IIS operations can be completed using the functions in system. directoryservices. The MIME type is also acceptable. Sure enough, I found it in msdn. The original text is as follows: http://msdn2.microsoft.com/en-us/library/ms525901.aspx
The key step here is to referenceActive ds iis namespace providerIn this way, you can useIisoleThis namespace, andIismimetypeThis class.
The actual code is not complex. All of them are below. It is relatively simple and I will not explain it much.
Code
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 );
Try
{
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)
{
Propvalues. Remove (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;
Propvalues. Add (newobj );
Path. commitchanges ();
Console. writeline ("done .");
}
Catch (exception ex)
{
If ("hresult 0x80005006" = ex. Message)
Console. writeline ("Property mimemap does not exist at {0}", metabasepath );
Else
Console. writeline ("failed in setmimetypeproperty with the following exception: \ n {0}", Ex. Message );
}
}
}
}