) Add the MIME type to IIS by programming.

Source: Internet
Author: User

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

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.