How can I set the ehe .net framework version and the managed pipeline mode programmatically for a IIS 7 programmatic via C#? What a the metabase property names for that?
views:
15answers:
1
+2
A:
You could use the Microsoft.Web.Administration assembly. Here's how you could set the framework version:
using (var manager = new ServerManager())
{
// Get the web site given its unique id
var site = manager.Sites.Cast<Site>().Where(s => s.Id == 1).FirstOrDefault();
if (site == null)
{
throw new Exception("The site with ID = 1 doesn't exist");
}
// get the application you want to set the framework version to
var application = site.Applications["/vDirName"];
if (application == null)
{
throw new Exception("The virtual directory /vDirName doesn't exist");
}
// get the corresponding application pool
var applicationPool = manager.ApplicationPools
.Cast<Microsoft.Web.Administration.ApplicationPool>()
.Where(appPool => appPool.Name == application.ApplicationPoolName)
.FirstOrDefault();
if (applicationPool == null)
{
// normally this should never happen
throw new Exception("The virtual directory /vDirName doesn't have an associated application pool");
}
applicationPool.ManagedRuntimeVersion = "v4.0.30319";
manager.CommitChanges();
}
And here's how to set the managed pipeline mode to integrated:
using (var manager = new ServerManager())
{
// Get the application pool given its name
var appPool = manager.ApplicationPools["AppPoolName"];
if (appPool == null)
{
throw new Exception("The application pool AppPoolName doesn't exist");
}
// set the managed pipeline mode
appPool.ManagedPipelineMode = ManagedPipelineMode.Integrated;
// save
manager.CommitChanges();
}
Darin Dimitrov
2010-10-19 11:45:02
Thank you for your answer. But because of some reasons I cannot use Microsoft.Web.Administration. I must do it with DirectoryEntry
Elmex
2010-10-19 12:16:54
What are those reasons?
Darin Dimitrov
2010-10-19 12:17:27
I am developing under Windows XP, but the software is running on a Windows 2008 server. Nevertheless, I have already found the solution for myself.
Elmex
2010-10-19 12:26:11