C# 操作IIS方法集合

时间:2021-09-26 17:31:33

如果在win8,win7情况下报错:未知错误(0x80005000)

见http://blog.csdn.net/ts1030746080/article/details/8741399

 

 

  1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.DirectoryServices;
5 using System.Linq;
6 using System.Net;
7 using System.Text;
8 using System.Threading.Tasks;
9
10 namespace IISControlHelper
11 {
12 /// <summary>
13 /// IIS 操作方法集合
14 /// http://blog.csdn.net/ts1030746080/article/details/8741399 错误
15 /// </summary>
16 public class IISWorker
17 {
18 private static string HostName = "localhost";
19
20 /// <summary>
21 /// 获取本地IIS版本
22 /// </summary>
23 /// <returns></returns>
24 public static string GetIIsVersion()
25 {
26 try
27 {
28 DirectoryEntry entry = new DirectoryEntry("IIS://" + HostName + "/W3SVC/INFO");
29 string version = entry.Properties["MajorIISVersionNumber"].Value.ToString();
30 return version;
31 }
32 catch (Exception se)
33 {
34 //说明一点:IIS5.0中没有(int)entry.Properties["MajorIISVersionNumber"].Value;属性,将抛出异常 证明版本为 5.0
35 return string.Empty;
36 }
37 }
38
39 /// <summary>
40 /// 创建虚拟目录网站
41 /// </summary>
42 /// <param name="webSiteName">网站名称</param>
43 /// <param name="physicalPath">物理路径</param>
44 /// <param name="domainPort">站点+端口,如192.168.1.23:90</param>
45 /// <param name="isCreateAppPool">是否创建新的应用程序池</param>
46 /// <returns></returns>
47 public static int CreateWebSite(string webSiteName, string physicalPath, string domainPort,bool isCreateAppPool)
48 {
49 DirectoryEntry root = new DirectoryEntry("IIS://" + HostName + "/W3SVC");
50 // 为新WEB站点查找一个未使用的ID
51 int siteID = 1;
52 foreach (DirectoryEntry e in root.Children)
53 {
54 if (e.SchemaClassName == "IIsWebServer")
55 {
56 int ID = Convert.ToInt32(e.Name);
57 if (ID >= siteID) { siteID = ID + 1; }
58 }
59 }
60 // 创建WEB站点
61 DirectoryEntry site = (DirectoryEntry)root.Invoke("Create", "IIsWebServer", siteID);
62 site.Invoke("Put", "ServerComment", webSiteName);
63 site.Invoke("Put", "KeyType", "IIsWebServer");
64 site.Invoke("Put", "ServerBindings", domainPort + ":");
65 site.Invoke("Put", "ServerState", 2);
66 site.Invoke("Put", "FrontPageWeb", 1);
67 site.Invoke("Put", "DefaultDoc", "Default.html");
68 // site.Invoke("Put", "SecureBindings", ":443:");
69 site.Invoke("Put", "ServerAutoStart", 1);
70 site.Invoke("Put", "ServerSize", 1);
71 site.Invoke("SetInfo");
72 // 创建应用程序虚拟目录
73
74 DirectoryEntry siteVDir = site.Children.Add("Root", "IISWebVirtualDir");
75 siteVDir.Properties["AppIsolated"][0] = 2;
76 siteVDir.Properties["Path"][0] = physicalPath;
77 siteVDir.Properties["AccessFlags"][0] = 513;
78 siteVDir.Properties["FrontPageWeb"][0] = 1;
79 siteVDir.Properties["AppRoot"][0] = "LM/W3SVC/" + siteID + "/Root";
80 siteVDir.Properties["AppFriendlyName"][0] = "Root";
81
82 if (isCreateAppPool)
83 {
84 DirectoryEntry apppools = new DirectoryEntry("IIS://" + HostName + "/W3SVC/AppPools");
85
86 DirectoryEntry newpool = apppools.Children.Add(webSiteName, "IIsApplicationPool");
87 newpool.Properties["AppPoolIdentityType"][0] = "4"; //4
88 newpool.Properties["ManagedPipelineMode"][0] = "0"; //0:集成模式 1:经典模式
89 newpool.CommitChanges();
90 siteVDir.Properties["AppPoolId"][0] = webSiteName;
91 }
92
93 siteVDir.CommitChanges();
94 site.CommitChanges();
95 return siteID;
96 }
97
98 /// <summary>
99 /// 得到网站的物理路径
100 /// </summary>
101 /// <param name="rootEntry">网站节点</param>
102 /// <returns></returns>
103 public static string GetWebsitePhysicalPath(DirectoryEntry rootEntry)
104 {
105 string physicalPath = "";
106 foreach (DirectoryEntry childEntry in rootEntry.Children)
107 {
108 if ((childEntry.SchemaClassName == "IIsWebVirtualDir") && (childEntry.Name.ToLower() == "root"))
109 {
110 if (childEntry.Properties["Path"].Value != null)
111 {
112 physicalPath = childEntry.Properties["Path"].Value.ToString();
113 }
114 else
115 {
116 physicalPath = "";
117 }
118 }
119 }
120 return physicalPath;
121 }
122
123 /// <summary>
124 /// 获取站点名
125 /// </summary>
126 public static List<IISInfo> GetServerBindings()
127 {
128 List<IISInfo> iisList = new List<IISInfo>();
129 string entPath = String.Format("IIS://{0}/w3svc", HostName);
130 DirectoryEntry ent = new DirectoryEntry(entPath);
131 foreach (DirectoryEntry child in ent.Children)
132 {
133 if (child.SchemaClassName.Equals("IIsWebServer", StringComparison.OrdinalIgnoreCase))
134 {
135 if (child.Properties["ServerBindings"].Value != null)
136 {
137 object objectArr = child.Properties["ServerBindings"].Value;
138 string serverBindingStr = string.Empty;
139 if (IsArray(objectArr))//如果有多个绑定站点时
140 {
141 object[] objectToArr = (object[])objectArr;
142 serverBindingStr = objectToArr[0].ToString();
143 }
144 else//只有一个绑定站点
145 {
146 serverBindingStr = child.Properties["ServerBindings"].Value.ToString();
147 }
148 IISInfo iisInfo = new IISInfo();
149 iisInfo.DomainPort = serverBindingStr;
150 iisInfo.AppPool = child.Properties["AppPoolId"].Value.ToString();//应用程序池
151 iisList.Add(iisInfo);
152 }
153 }
154 }
155 return iisList;
156 }
157
158
159 public static bool CreateAppPool(string appPoolName, string Username, string Password)
160 {
161 bool issucess = false;
162 try
163 {
164 //创建一个新程序池
165 DirectoryEntry newpool;
166 DirectoryEntry apppools = new DirectoryEntry("IIS://" + HostName + "/W3SVC/AppPools");
167 newpool = apppools.Children.Add(appPoolName, "IIsApplicationPool");
168
169 //设置属性 访问用户名和密码 一般采取默认方式
170 newpool.Properties["WAMUserName"][0] = Username;
171 newpool.Properties["WAMUserPass"][0] = Password;
172 newpool.Properties["AppPoolIdentityType"][0] = "3";
173 newpool.CommitChanges();
174 issucess = true;
175 return issucess;
176 }
177 catch // (Exception ex)
178 {
179 return false;
180 }
181 }
182
183
184 /// <summary>
185 /// 建立程序池后关联相应应用程序及虚拟目录
186 /// </summary>
187 public static void SetAppToPool(string appname,string poolName)
188 {
189 //获取目录
190 DirectoryEntry getdir = new DirectoryEntry("IIS://localhost/W3SVC");
191 foreach (DirectoryEntry getentity in getdir.Children)
192 {
193 if (getentity.SchemaClassName.Equals("IIsWebServer"))
194 {
195 //设置应用程序程序池 先获得应用程序 在设定应用程序程序池
196 //第一次测试根目录
197 foreach (DirectoryEntry getchild in getentity.Children)
198 {
199 if (getchild.SchemaClassName.Equals("IIsWebVirtualDir"))
200 {
201 //找到指定的虚拟目录.
202 foreach (DirectoryEntry getsite in getchild.Children)
203 {
204 if (getsite.Name.Equals(appname))
205 {
206 //【测试成功通过】
207 getsite.Properties["AppPoolId"].Value = poolName;
208 getsite.CommitChanges();
209 }
210 }
211 }
212 }
213 }
214 }
215 }
216
217
218 /// <summary>
219 /// 判断object对象是否为数组
220 /// </summary>
221 public static bool IsArray(object o)
222 {
223 return o is Array;
224 }
225 }
226 }