Link to home
Start Free TrialLog in
Avatar of PhobiaDHS
PhobiaDHS

asked on

How to programmatically detect .NET version assigned to an application pool in IIS 6,7, and 7.5?

Hey there everyone. I am working on a project that requires me to detect the .NET version that is assigned to an application pool. The project is being written in C++ with a few C# areas but can call any number of other files or languages if there is a need. The only thing is, everything that it calls has to be available on a standard web server installation of Windows 2003(R2), 2008 and 2008R2.
We found a VBScript file that works somewhat well on Windows 2003, but we would much prefer to avoid this and use an ADSI call or something similar. The problem is that ADSI doesn't have the capabilities to do what we are trying to make it do.
Please let me know if you need any other information in order to provide a possible answer.
Thanks a bunch everyone.
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

u can either use appcmd.exe to configure the app pool or programically by using ServerManager (namespace Microsoft.Web.Administration)

for both options check the following post:
http://www.iis.net/ConfigReference/system.applicationHost/applicationPools/applicationPoolDefaults
for example to assign .net v2.0 to application pool "DefaultAppPool":

appcmd.exe set config -section:system.applicationHost/applicationPools /+"[name='DefaultAppPool',managedRuntimeVersion='v2.0]" /commit:apphost

or using C#:

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample {

   private static void Main() {

      using(ServerManager serverManager = new ServerManager()) { 
      Configuration config = serverManager.GetApplicationHostConfiguration();

      ConfigurationSection applicationPoolsSection = config.GetSection("system.applicationHost/applicationPools");

      ConfigurationElementCollection applicationPoolsCollection = applicationPoolsSection.GetCollection();

      ConfigurationElement addElement = applicationPoolsCollection.CreateElement("add");
      addElement["name"] = @"DefaultAppPool";
      addElement["managedRuntimeVersion"] = @"v2.0";
      applicationPoolsCollection.Add(addElement);

      serverManager.CommitChanges();
      }
   }
}

Open in new window

Avatar of PhobiaDHS
PhobiaDHS

ASKER

As the answer applied to a different question, I would like to suggest closing with no points assigned.
As the answer applied to a different question, I would like to suggest closing with no points assigned.
What I mean is that per my understanding of the code given for the solution, the solution doesn't allow detection of the .NET version assigned to an app pool. It allows you to set it. If I am incorrect on this, please split and assign. My current understanding is that this does not apply to the question that was asked.
ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you can detect this configuration by using the following as well:

using (ServerManager serverManager = new ServerManager())
            {
                Configuration config = serverManager.GetApplicationHostConfiguration();

                ConfigurationSection applicationPoolsSection = config.GetSection("system.applicationHost/applicationPools");

                ConfigurationElementCollection applicationPoolsCollection = applicationPoolsSection.GetCollection();
                string ver = applicationPoolsCollection.ChildElements[0].Attributes["managedRuntimeVersion"].Value.ToString();
            }

Open in new window

I have tested this and it works like a champ. Sorry for the confusion and thanks for following up with the extra information.
@PhobiaDHS  
no problem :)