当前位置: 首页 > 编程语言 > C# > 正文

C#操作IIS创建应用程序池时出现无效索引异常及解决方法

时间:2014-10-13

C#操作IIS创建应用程序池出现异常:无效索引(Exception from HRESULT:0x80070585)

相关代码:

01.public static string CreateAppPool(string appPoolName, string frameworkVersion, string managedPipelineMode)  
02. {
03. DirectoryEntry rootfolder = new DirectoryEntry("IIS://localhost/W3SVC/APPPOOLS");
04.
05. try
06. {
07. DirectoryEntry appPool = rootfolder.Children.Add(appPoolName, "IIsApplicationPool");
08. if (appPool.Properties.Contains("AppPoolCommand"))
09. {
10. appPool.Properties["AppPoolCommand"][0] = "1";
11. }
12. if (appPool.Properties.Contains("ManagedRuntimeVersion"))
13. {
14. appPool.Properties["ManagedRuntimeVersion"][0] = "v4.0";
15. }
16. //以下代码在WindowsServer2008版本为6.0的系统上会出现异常:
17. //无效索引(Exception from HRESULT:0x80070585)
18. //if (appPool.Properties.Contains("AppPoolIdentityType"))
19. //{
20. // appPool.Properties["AppPoolIdentityType"][0] = "4";
21. //}
22. if (appPool.Properties.Contains("Enable32BitAppOnWin64"))
23. {
24. appPool.Properties["Enable32BitAppOnWin64"][0] = true;
25. }
26. appPool.CommitChanges();
27. return null;
28. }
29. catch (System.Exception ex)
30. {
31. LogToFile.Save(ex, "CreateAppPool");
32. return ex.Message;
33. }
34. }

将相关代码注释以后,未见对Web应用程序带来任何影响。

URL:http://www.bianceng.cn/Programming/csharp/201410/45785.htm