简体   繁体   中英

How to parse the IIS ApplicationHost.config file into .Net objects?

I am writing an app to interrogate what sites are active on an IIS server. The best way seems to be to directly read the ApplicationHost.config file.

What's the easiest way to go about this?

Is it possible to write back a new site using this method also?

1) ApplicationHost.config is an XML file so you can simply use the XDocument class.

2) Yes - XDocument allows construction of new XML elements.

Since the ApplicationHost.Config is just an XML, you could use some tool to generate classes from it, something like Xml2CSharp . You may need to adapt one thing or another, but it's a start.

Then you deserialize the ApplicationHost.config into objects like a regular XML.

You could directly change the ApplicationHost.config to add sites, application pools and change other configs, but I wouldn't recommend to do so.

You can use the ServerManager class. Here you are a very basic sample, so you can start playing around:

using(var serverManager = ServerManager.OpenRemote("my-remote-server"))
{
    if (serverManager.Sites.AllowsAdd())
    {
        var site = serverManager.Sites.Add(siteName, path, port);
        // use site for something, like changing its bindings or something else
        serverManager.CommitChanges(); // without this, changes are made only in memory
    }
}

Make sure you the application which will execute this code runs under a user which exists in the remote server. The application needs to run in a machine and user account which can remotely access the target IIS server.

Also, you need to add a reference to Microsoft.Web.Administration , should be located under c:\\windows\\system32\\inetsrv.

I generated classes with Xml2CSharp with the applicationHost.config file I had. I also was only interested in bindings per site. The main changes were to correct class and variable names with full stops (system.webServer -> systemwebServer). Stackoverflow won't host uploaded class files and at 815 lines the generated class was a bit long to post. However to get started this may help

    JavaScriptSerializer ser = new JavaScriptSerializer();

string applicationHostFile = @"C:\Windows\System32\inetsrv\Config\applicationHost.config";

using (System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(applicationHostFile))
{
    System.Xml.Serialization.XmlSerializer DeSerializer = new System.Xml.Serialization.XmlSerializer(typeof(Xml2CSharp.Configuration));
    Xml2CSharp.Configuration configuration = (Xml2CSharp.Configuration)DeSerializer.Deserialize(reader);

    foreach (var site in configuration.SystemapplicationHost.Sites.Site)
    {
        // clear Application to not expose sensitive information
        site.Application = null;
        Console.WriteLine(site.Name);
        foreach (var binding in site.Bindings.Binding)
        {
            Console.WriteLine(binding.BindingInformation);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM