Adding MIME types to IIS programmatically

Source: Internet
Author: User

A recent setup, one step, is to add a new MIME type to IIS. Log so that users can download IIS logs.



All of the operations of IIS can be done with the functionality in System.DirectoryServices. MIME types must also be. Sure enough, after a rummage search, found in MSDN. The original text is as follows: http://msdn2.microsoft.com/en-us/library/ms525901.aspx

The key step here is to refer to active DS IIS Namespace Provider So you can use the Iisole namespace, and IIsMimeType this class.



The actual code is not complex, all in the following, relatively simple, not much explanation.
Copy C # code save 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[] ar
        GS) {setmimetypeproperty ("Iis://localhost/w3svc/1/root", ". Hlp", "Application/winhlp");
            static void Setmimetypeproperty (String metabasepath, String newextension, String newmimetype) { MetabasePath is to the form "iis://<servername>/<path>"//For example "Iis://loca      
            Lhost/w3svc/1/root '//newextension is of the form ' .<extension> ' for example, '. HLP ' Newmimetype is of the form "<application>", for example, "Application/winhlp" Console.wri
Teline ("/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."); The 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.