|
C#对IIS进行管理,管理代码:
View Code
1 using System;
2 using System.Collections;
3 using System.DirectoryServices;
4 using Microsoft.Win32;
5
6 namespace SCL
7 {
8 public class IISHelper
9 {
10 static DirectoryEntry iisDE = new DirectoryEntry("IIS://localhost/W3SVC");
11
12 //判断 IIS是否安装
13 public static bool IsIISSetup
14 {
15 get
16 {
17 object obj=Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup\Oc Manager\Subcomponents", "iis_inetmgr", "0");
18 if (obj == null) return false;
19
20 if(obj.ToString ().Equals("1"))
21 return true;
22 else
23 return false;
24 }
25 }
26
27 ///<summary>
28 /// Get The Location IIS WebServers Information
29 ///</summary>
30 ///<returns></returns>
31 public static Hashtable GetLocationIIsWebServers()
32 {
33 Hashtable result = new Hashtable();
34 GetWebSiteInfo(ref result);
35 return result;
36 }
37
38 /// <summary>
39 /// 获取服务器IIS版本
40 /// </summary>
41 /// <param name="DomainName"></param>
42 /// <returns></returns>
43 public static WebServerTypes GetIISServerType(string DomainName)
44 {
45 if (string.IsNullOrEmpty(DomainName))
46 {
47 DomainName = "LOCALHOST";
48 }
49 string path = "IIS://" + DomainName + "/W3SVC/INFO";
50 DirectoryEntry entry = null;
51 try
52 {
53 entry = new DirectoryEntry(path);
54 }
55 catch
56 {
57 return WebServerTypes.Unknown;
58 }
59 int num = 5;
60 try
61 {
62 num = (int)entry.Properties["MajorIISVersionNumber"].Value;
63 }
64 catch
65 {
66 return WebServerTypes.IIS5;
67 }
68 switch (num)
69 {
70 case 6:
71 return WebServerTypes.IIS6;
72
73 case 7:
74 return WebServerTypes.IIS7;
75 }
76 return WebServerTypes.IIS6;
77 }
78
79 ///<summary>
80 /// Create a new IIS Webserver
81 ///</summary>
82 ///<param name="webServerName">webserver name</param>
83 ///<param name="path">webserver directory path</param>
84 ///<param name="port">access port</param>
85 public static void CreateNewIIsWebServer(string webServerName, string path, int port)
86 {
87 int siteID = GetWebSiteInfo(port);
88 using (DirectoryEntry site = (DirectoryEntry)iisDE.Invoke("Create", "IIsWebServer", siteID))
89 {
90 site.Invoke("Put", "ServerComment", webServerName);
91 site.Invoke("Put", "KeyType", "IIsWebServer");
92 site.Invoke("Put", "ServerBindings", ":" + port.ToString() + ":");
93 site.Invoke("Put", "ServerState", 2);
94 site.Invoke("Put", "FrontPageWeb", 1);
95 site.Invoke("Put", "DefaultDoc", "Default.aspx");
96 site.Invoke("Put", "SecureBindings", ":443:");
97 site.Invoke("Put", "ServerAutoStart", 1);
98 site.Invoke("Put", "ServerSize", 1);
99 site.Invoke("SetInfo");
100 using (DirectoryEntry siteVDir = site.Children.Add("Root", "IISWebVirtualDir"))
101 {
102 siteVDir.Properties["AppIsolated"][0] = 2;
103 siteVDir.Properties["Path"][0] = path;
104 siteVDir.Properties["AccessFlags"][0] = 513;
105 siteVDir.Properties["FrontPageWeb"][0] = 1;
106 //siteVDir.Properties["AppRoot"][0] = "LM/W3SVC/" + siteID + "/"+webServerName;
107 siteVDir.Properties["AppFriendlyName"][0] = webServerName;
108 siteVDir.Invoke("AppCreate", true);
109 siteVDir.CommitChanges();
110 }
111 site.CommitChanges();
112 }
113
114 }
115
116 ///<summary>
117 /// Create a new virtual directory
118 ///</summary>
119 ///<param name="website">webserver name</param>
120 ///<param name="dirName">virtual directory name</param>
121 ///<param name="properties">virtual dirctory properties</param>
122 public static void CreateNewVirtualDir(string website, string dirName, System.Data.PropertyCollection properties)
123 {
124 if (GetVirtualDir(website, dirName)) throw new Exception(" The Virtual Dir is alread existed");
125 using (DirectoryEntry de = GetWebSiteInfo(website))
126 {
127 using (DirectoryEntry vde = de.Children.Add(dirName, "IIsWebVirtualDir"))
128 {
129 vde.CommitChanges();
130 de.CommitChanges();
131 UpdateVDirInfo(vde, ref properties);
132 vde.Invoke("AppCreate", true);
133 vde.CommitChanges();
134 de.CommitChanges();
135 }
136 }
137 }
138
139 ///<summary>
140 /// Get Common virtual directory setting
141 ///</summary>
142 ///<param name="path">virtual directory path</param>
143 ///<param name="vdir">virtual directory name</param>
144 ///<returns></returns>
145 public static System.Data.PropertyCollection GetDefaultVirtualDirSetting(string path, string vdir)
146 {
147 System.Data.PropertyCollection vProperty = new System.Data.PropertyCollection();
148 //vProperty.Add("AnonymousUserName","");
149 //vProperty.Add("AnonymousUserPass","");
150 vProperty.Add("AccessRead", true);
151 vProperty.Add("AccessExecute", false);
152 vProperty.Add("AuthBasic", true);
153 vProperty.Add("AuthNTLM", true);
154 vProperty.Add("ContentIndexed", true);
155 vProperty.Add("EnableDefaultDoc", true);
156 vProperty.Add("EnableDirBrowsing", false);
157 vProperty.Add("AccessSSL", false);
158 vProperty.Add("AccessScript", false);
159 vProperty.Add("DefaultDoc", "Default.aspx");
160 vProperty.Add("Path", path);
161 vProperty.Add("AppIsolated", 2);
162 vProperty.Add("AppFriendlyName", vdir);
163 vProperty.Add("AccessFlags", 513);
164 vProperty.Add("FrontPageWeb", 1);
165 //vProperty.Add("DontLog", true);
166 //vProperty.Add("AppRoot", "LM/W3SVC/" + siteID + "/" + vdir);
167 return vProperty;
168 }
169
170 ///<summary>
171 /// Delete the virtual directory in the WebServer
172 ///</summary>
173 ///<param name="website">webserver name</param>
174 ///<param name="vdir">virtual directory name</param>
175 public static void DeleteVirtualDir(string website, string vdir)
176 {
177 if (!GetVirtualDir(website, vdir)) throw new Exception(" The virtual directory don't exist in the website");
178 using (DirectoryEntry de = GetWebSiteInfo(website))
179 {
180 foreach (DirectoryEntry sub in de.Children)
181 {
182 if (sub.Name == vdir)
183 {
184 de.Invoke("Delete", new string[] { sub.SchemaClassName, vdir });
185 de.CommitChanges();
186 }
187 }
188 }
189 }
190
191
192 private static void UpdateVDirInfo(DirectoryEntry newDE, ref System.Data.PropertyCollection properties)
193 {
194 //newDE.Properties["AnonyMousUserName"][0] = properties["AnonymousUserName"].ToString();
195 //newDE.Properties["AnonymousUserPass"][0] = properties["AnonymousUserPass"].ToString();
196 newDE.Properties["AccessRead"][0] = (bool)properties["AccessRead"];
197 newDE.Properties["AccessExecute"][0] = (bool)properties["AccessExecute"];
198 newDE.Properties["AuthBasic"][0] = (bool)properties["AuthBasic"];
199 newDE.Properties["AuthNTLM"][0] = (bool)properties["AuthNTLM"];
200 newDE.Properties["ContentIndexed"][0] = (bool)properties["ContentIndexed"];
201 newDE.Properties["EnableDefaultDoc"][0] = (bool)properties["EnableDefaultDoc"];
202 newDE.Properties["EnableDirBrowsing"][0] = (bool)properties["EnableDirBrowsing"];
203 newDE.Properties["AccessSSL"][0] = (bool)properties["AccessSSL"];
204 newDE.Properties["AccessScript"][0] = (bool)properties["AccessScript"];
205 newDE.Properties["DefaultDoc"][0] = properties["DefaultDoc"].ToString();
206 newDE.Properties["Path"][0] = properties["Path"].ToString();
207 newDE.Properties["AppIsolated"][0] = (int)properties["AppIsolated"];
208 newDE.Properties["AppFriendlyName"][0] = properties["AppFriendlyName"].ToString();
209 newDE.Properties["AccessFlags"][0] = (int)properties["AccessFlags"];
210 newDE.Properties["FrontPageWeb"][0] = (int)properties["FrontPageWeb"];
211 //newDE.Properties["DontLog"][0] = (bool)properties["DontLog"];
212 //newDE.Properties["AppRoot"][0] = properties["AppRoot"].ToString();
213 }
214
215 public static bool GetVirtualDir(string webSite, string dirName)
216 {
217 bool result = false;
218 using (DirectoryEntry de = GetWebSiteInfo(webSite))
219 {
220
221 if (de != null)
222 {
223 foreach (DirectoryEntry subVD in de.Children)
224 {
225 if (subVD.SchemaClassName == "IIsWebVirtualDir" && subVD.Name == dirName)
226 {
227 result = true;
228 break;
229 }
230 }
231 }
232 }
233 return result;
234 }
235
236 private static void GetWebSiteInfo(ref Hashtable webServer)
237 {
238 DirectoryEntries des = iisDE.Children;
239 foreach (DirectoryEntry subDE in des)
240 {
241 if (subDE.SchemaClassName == "IIsWebServer")
242 {
243 webServer.Add(subDE.Properties["ServerComment"].Value.ToString(), subDE.Name);
244 }
245 }
246 des = null;
247 }
248
249 private static DirectoryEntry GetWebSiteInfo(string website)
250 {
251 DirectoryEntry result = null;
252 DirectoryEntries des = iisDE.Children;
253 foreach (DirectoryEntry subDE in des)
254 {
255 if (subDE.SchemaClassName == "IIsWebServer" && subDE.Properties["ServerComment"].Value.ToString() == website)
256 {
257 result = subDE.Children.Find("Root", "IIsWebVirtualDir");
258 break;
259 }
260 }
261 des = null;
262 return result;
263 }
264
265 private static int GetWebSiteInfo(int port)
266 {
267 int result = 1, i = 1;
268 DirectoryEntries des = iisDE.Children;
269 foreach (DirectoryEntry subDE in des)
270 {
271 if (subDE.SchemaClassName == "IIsWebServer")
272 {
273 if ((i = Convert.ToInt32(subDE.Name)) >= result)
274 {
275 result = i + 1;
276 }
277 if (subDE.Properties["ServerBindings"][0].ToString() == ":" + port.ToString() + ":")
278 {
279 throw new Exception(" The port is already used");
280 }
281 }
282 }
283 des = null;
284 return result;
285 }
286 }
287
288 public enum WebServerTypes
289 {
290 Unknown,
291 IIS5,
292 IIS6,
293 IIS7
294 }
295 }
C#对IIS进行管理,事例代码:
View Code
1 if (!IISHelper.IsIISSetup)
2 {
3 //MessageBox.Show("请先安装IIS服务,再进行此操作");
4 return;
5 }
6
7 #region SclService
8
9 string serpath = System.Environment.CurrentDirectory + "\\SCLService";
10 if (System.IO.Directory.Exists(serpath))
11 {
12 string Name = "SclService";
13 string website = "默认网站";
14 System.Data.PropertyCollection properties = IISHelper.GetDefaultVirtualDirSetting(serpath, Name);
15
16 if (IISHelper.GetVirtualDir(website, Name))
17 {
18 //DialogResult dr = MessageBox.Show("WEB数据服务器 虚拟目录已经存在,是否删除重新创建?", "信息提示", MessageBoxButtons.YesNo);
19 //if (dr == DialogResult.Yes)
20 //{
21 IISHelper.DeleteVirtualDir(website, Name);
22 IISHelper.CreateNewVirtualDir(website, Name, properties);
23 // }
24 }
25 else
26 {
27 IISHelper.CreateNewVirtualDir(website, Name, properties);
28 }
29 }
30 System.IO.DirectoryRights.AddDirectoryRights(serpath, "ASPNET", "FullControl");
31 System.IO.DirectoryRights.AddDirectoryRights(serpath, "Everyone", "FullControl");
32 System.IO.DirectoryRights.AddDirectoryRights(serpath, "Users", "FullControl");
33 #endregion
34
35 #region SCLWeb
36 string webpath = System.Environment.CurrentDirectory + "\\SCLWeb";
37 if (System.IO.Directory.Exists(webpath))
38 {
39 string Name = "SclWeb";
40 string website = "默认网站";
41
42 System.Data.PropertyCollection properties = IISHelper.GetDefaultVirtualDirSetting(webpath, Name);
43
44 if (IISHelper.GetVirtualDir(website, Name))
45 {
46 //DialogResult dr = MessageBox.Show("WEB数据发布 虚拟目录已经存在,是否删除重新创建?", "信息提示", MessageBoxButtons.YesNo);
47 //if (dr == DialogResult.Yes)
48 //{
49 IISHelper.DeleteVirtualDir(website, Name);
50 IISHelper.CreateNewVirtualDir(website, Name, properties);
51 //}
52 }
53 else
54 {
55 IISHelper.CreateNewVirtualDir(website, Name, properties);
56 }
57 }
官方下载
本地下载
NET破解专区 |
|