在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1 using System; 2 using System.DirectoryServices; 3 using System.Management; 4 using Microsoft.Web.Administration; 5 6 namespace ConsoleApplication_IsNullTest 7 { 8 /// <summary> 9 /// IIS Helper. 10 /// </summary> 11 public class IISHelper 12 { 13 14 private static string siteName = "siteName"; 15 private static string sitePath = "sitePath"; 16 private static string serverId = "001"; 17 private static string runtimeVersion="v4.0"; 18 19 public static string RuntimeVersion 20 { 21 get { return runtimeVersion; } 22 } 23 24 public static DirectoryEntry iisSite = new DirectoryEntry("IIS://localhost/W3SVC", "UserName", "Userpwd"); 25 26 private static ManagementScope scope = null; 27 28 public IISHelper() 29 { 30 scope = new ManagementScope("ConfigManager.ManagementScopePath"); 31 if (scope.IsConnected == false) 32 { 33 scope.Connect(); 34 } 35 } 36 37 /// <summary> 38 /// 创建应用程序池 39 /// </summary> 40 /// <param name="AppPoolName">程序池名</param> 41 /// <param name="PipelineModel">Managed pipeline model.</param> 42 public static void CreateAppPool(string AppPoolName, ManagedPipelineMode pipelineMode) 43 { 44 DirectoryEntry newpool; 45 DirectoryEntry apppools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools"); 46 newpool = apppools.Children.Add(AppPoolName, "IIsApplicationPool"); 47 newpool.Properties["ManagedRuntimeVersion"].Value = runtimeVersion; 48 newpool.Properties["ManagedPipelineMode"].Value = ManagedPipelineMode.Integrated; 49 newpool.CommitChanges(); 50 } 51 52 /// <summary> 53 /// 创建site 54 /// </summary> 55 public static void CreateWebSite(Object site) 56 { 57 ManagementObject manage = new ManagementObject(scope, new ManagementPath(@"IIsWebService='W3SVC'"), null); 58 ManagementBaseObject inputParameters = manage.GetMethodParameters("CreateNewSite"); 59 ManagementBaseObject[] serverBinding = new ManagementBaseObject[1]; 60 serverBinding[0] = CreateServerBinding("", "127.0.0.1", "8081"); 61 inputParameters["ServerComment"] = siteName; 62 inputParameters["ServerBindings"] = serverBinding; 63 inputParameters["PathOfRootVirtualDir"] = sitePath; 64 inputParameters["ServerId"] = serverId; 65 try 66 { 67 manage.InvokeMethod("CreateNewSite", inputParameters, null); 68 } 69 catch 70 { 71 throw new Exception("createNewSite"); 72 } 73 SetAppToPool(siteName, serverId); 74 75 System.Threading.Thread.Sleep(3000); 76 77 //启动web site 78 string serverName = "W3SVC/" + serverId; 79 ManagementObject manageSite = new ManagementObject(scope, new ManagementPath(@"IIsWebServer='" + serverName + "'"), null); 80 manageSite.InvokeMethod("Start", null); 81 } 82 83 /// <summary> 84 /// 关联应用程序池 85 /// </summary> 86 /// <param name="appPoolName"></param> 87 /// <param name="serverId"></param> 88 private static void SetAppToPool(string appPoolName, string serverId) 89 { 90 string path = string.Format("IIS://localhost/W3SVC/{0}/Root", serverId); 91 DirectoryEntry entry = new DirectoryEntry(path); 92 entry.Properties["AppPoolId"].Value = appPoolName; 93 entry.CommitChanges(); 94 entry.Close(); 95 } 96 97 /// <summary> 98 /// Get site url前缀 99 /// </summary> 100 /// <param name="HostName"></param> 101 /// <param name="IP"></param> 102 /// <param name="Port"></param> 103 /// <returns></returns> 104 private static ManagementObject CreateServerBinding(string HostName, string IP, string Port) 105 { 106 try 107 { 108 ManagementClass classBinding = new ManagementClass(scope, new ManagementPath("ServerBinding"), null); 109 ManagementObject serverBinding = classBinding.CreateInstance(); 110 serverBinding.Properties["Hostname"].Value = HostName; 111 serverBinding.Properties["IP"].Value = IP; 112 serverBinding.Properties["Port"].Value = Port; 113 serverBinding.Put(); 114 return serverBinding; 115 } 116 catch 117 { 118 return null; 119 } 120 } 121 122 /// <summary> 123 /// 创建web site 子项的site $ 应用程序池 124 /// </summary> 125 /// <param name="site"></param> 126 /// <param name="serviceName"></param> 127 /// <param name="AppolName"></param> 128 public static void CreateApplication(Object site, string serviceName, string AppolName) 129 { 130 DirectoryEntry defaultWebSite = GetWebisteDirectory("FUSiteName");//父级site name 131 if (defaultWebSite != null) 132 { 133 DirectoryEntry defaultWebSiteRoot = new DirectoryEntry(defaultWebSite.Path + "/Root"); 134 135 //Create and setup new virtual directory 136 DirectoryEntry virtualDirectory = defaultWebSiteRoot.Children.Add(siteName, "IIsWebVirtualDir"); 137 138 virtualDirectory.Properties["Path"][0] = sitePath; 139 virtualDirectory.Properties["AppFriendlyName"][0] = siteName; 140 virtualDirectory.CommitChanges(); 141 142 virtualDirectory.Invoke("AppCreate", 1); 143 object[] param = { 0, siteName, true }; 144 virtualDirectory.Invoke("AppCreate3", param); 145 virtualDirectory.Properties["AppPoolId"].Value = AppolName; 146 147 string appPoolPath = @"IIS://localhost/W3SVC/AppPools/" + AppolName; 148 try 149 { 150 var appPoolEntry = new DirectoryEntry(appPoolPath); 151 appPoolEntry.Properties["ManagedRuntimeVersion"].Value = runtimeVersion; 152 appPoolEntry.Properties["ManagedPipelineMode"].Value = ManagedPipelineMode.Integrated; 153 appPoolEntry.Invoke("SetInfo", null); 154 appPoolEntry.CommitChanges(); 155 appPoolEntry.Close(); 156 } 157 catch 158 { 159 160 } 161 virtualDirectory.CommitChanges(); 162 virtualDirectory.Close(); 163 } 164 } 165 166 167 /// <summary> 168 /// Get web site directory. 169 /// </summary> 170 /// <param name="websiteName"></param> 171 /// <returns></returns> 172 private static DirectoryEntry GetWebisteDirectory(string websiteName) 173 { 174 DirectoryEntries des = iisSite.Children; 175 foreach (DirectoryEntry sub in des) 176 { 177 if (sub.SchemaClassName == "IIsWebServer" && sub.Properties["ServerComment"].Contains(websiteName.Trim())) 178 { 179 180 return sub; 181 } 182 } 183 return null; 184 } 185 } 186 }
|
请发表评论