SDK Sample: Changing the runtime version of an application pool in IIS 7
The following quick SDK sample demonstrates how to list and change the managed runtime version of application pools programatically in IIS 7.
[VB]
Imports System
Imports Microsoft.Web.Administration
Public Class AppPoolSample
Shared manager As ServerManager = New ServerManager()
' Main application processing
Public Shared Sub Main(ByVal args As String())
' Get the apppool to change
Dim iPool As Integer = GetAppPool()
' Get the framework version desired
Dim rtVersion As String = GetVersion()
' Set the apppool runtime
Dim poolToSet As ApplicationPool = manager.ApplicationPools(iPool)
Console.WriteLine("Setting application pool '{0}' to runtime version: {1}...", _
poolToSet.Name, rtVersion)
poolToSet.ManagedRuntimeVersion = rtVersion
' Commit the changes and recycle the application pool
manager.CommitChanges()
poolToSet.Recycle()
Console.WriteLine("Your changes have been committed.")
End Sub
' Prompts the user to select an application pool
Public Shared Function GetAppPool() As Integer
Dim pool As String = String.Empty
Dim iPool As Integer = 0
While (Not Integer.TryParse(pool, iPool))
Console.WriteLine("Available ApplicationPools: Managed runtime version")
Dim i As Integer
For i = 0 To manager.ApplicationPools.Count - 1 Step i + 1
Dim appPool As ApplicationPool = manager.ApplicationPools(i)
Console.WriteLine("{3}{0,3}.{3}{1}: {2}", i + 1, _
appPool.Name, appPool.ManagedRuntimeVersion, vbTab)
Next
Console.Write("{0}Choose an application pool to change: ", vbCrLf)
pool = Console.ReadLine()
End While
Return iPool - 1
End Function
' Prompts a user to select the version of runtime they would like
' the application pool to use
Public Shared Function GetVersion() As String
Dim rtVersion As String = String.Empty
Dim iVersion As Integer = 0
While (Not Integer.TryParse(rtVersion, iVersion))
Console.WriteLine("{0} 1.{0}Framework version 1.0", vbTab)
Console.WriteLine("{0} 2.{0}Framework version 1.1", vbTab)
Console.WriteLine("{0} 3.{0}Framework version 2.0", vbTab)
Console.Write("Choose the new managed runtime version: ")
rtVersion = Console.ReadLine()
End While
Select Case iVersion
Case 1
rtVersion = "v1.0"
Case 2
rtVersion = "v1.1"
Case 3
rtVersion = "v2.0"
End Select
Return rtVersion
End Function
End Class
[C#
using System;
using Microsoft.Web.Administration;
public class AppPoolSample
{
static ServerManager manager = new ServerManager();
// Main application processing
public static void Main(string[] args)
{
// Get the apppool to change
int iPool = GetAppPool();
// Get the framework version desired
string rtVersion = GetVersion();
// Set the apppool runtime
ApplicationPool poolToSet = manager.ApplicationPools[iPool];
Console.WriteLine("Setting application pool '{0}' to runtime version: {1}...",
poolToSet.Name, rtVersion);
poolToSet.ManagedRuntimeVersion = rtVersion;
// Commit the changes and recycle the application pool
manager.CommitChanges();
poolToSet.Recycle();
Console.WriteLine("Your changes have been committed.");
}
// Prompts the user to select an application pool
public static int GetAppPool()
{
string pool = String.Empty;
int iPool = 0;
while ((!int.TryParse(pool, out iPool)) ||
(iPool > manager.ApplicationPools.Count || iPool <= 0))
{
Console.WriteLine("Available ApplicationPools: Managed runtime version");
for (int i = 0; i <= manager.ApplicationPools.Count - 1; i++)
{
ApplicationPool appPool = manager.ApplicationPools[i];
Console.WriteLine("\t{0,3}.\t{1}: {2}", i + 1,
appPool.Name, appPool.ManagedRuntimeVersion);
}
Console.Write("\r\nChoose an application pool to change: ");
pool = Console.ReadLine();
}
return iPool -1;
}
// Prompts a user to select the version of runtime they would like
// the application pool to use
public static string GetVersion()
{
string rtVersion = String.Empty;
int iVersion = 0;
while ((!int.TryParse(rtVersion, out iVersion)) ||
(iVersion > 3 || iVersion < 1))
{
Console.WriteLine("\r\n\t 1.\tFramework version 1.0");
Console.WriteLine("\t 2.\tFramework version 1.1");
Console.WriteLine("\t 3.\tFramework version 2.0");
Console.Write("Choose the new managed runtime version: ");
rtVersion = Console.ReadLine();
}
switch (iVersion)
{
case 1:
rtVersion = "v1.0";
break;
case 2:
rtVersion = "v1.1";
break;
case 3:
rtVersion = "v2.0";
break;
}
return rtVersion;
}
}