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.
LVL 2
PhobiaDHSAsked:
Who is Participating?
 
Meir RivkinFull stack Software EngineerCommented:
@PhobiaDHS

my code snippet demonstrate the ServerManager usage by which u can configure different IIS properties.
one of them is using "managedRuntimeVersion" to set or get the .net version associated with the application pool (as shown in line 18).


 using(ServerManager serverManager = new ServerManager()) {

//to get the default .net version:
string ver = serverManager.ApplicationPoolDefaults.ManagedRuntimeVersion;

//to get specific app pool .net version:
ver =serverManager.ApplicationPools["AppPoolName"].ManagedRuntimeVersion;
}
0
 
Meir RivkinFull stack Software EngineerCommented:
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
0
 
Meir RivkinFull stack Software EngineerCommented:
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

0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
PhobiaDHSAuthor Commented:
As the answer applied to a different question, I would like to suggest closing with no points assigned.
0
 
PhobiaDHSAuthor Commented:
As the answer applied to a different question, I would like to suggest closing with no points assigned.
0
 
PhobiaDHSAuthor Commented:
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.
0
 
Meir RivkinFull stack Software EngineerCommented:
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

0
 
PhobiaDHSAuthor Commented:
I have tested this and it works like a champ. Sorry for the confusion and thanks for following up with the extra information.
0
 
Meir RivkinFull stack Software EngineerCommented:
@PhobiaDHS  
no problem :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.