-
Notifications
You must be signed in to change notification settings - Fork 1
/
IIS.cs
323 lines (301 loc) · 12 KB
/
IIS.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
using System;
using System.Collections.Generic;
using System.Linq;
namespace RX
{
public static class IIS
{
//需要引用Microsoft.Web.Administration.dll
//private static string Host = "localhost";
///// <summary>
///// 取得所有应用程序池
///// </summary>
///// <returns></returns>
//public static List<string> GetAppPools()
//{
// var appPools = new DirectoryEntry(string.Format("IIS://{0}/W3SVC/AppPools", Host));
// return (from DirectoryEntry entry in appPools.Children select entry.Name).ToList();
//}
///// <summary>
///// 取得单个应用程序池
///// </summary>
///// <returns></returns>
//public static ApplicationPool GetAppPool(string appPoolName)
//{
// ApplicationPool app = null;
// var appPools = new DirectoryEntry(string.Format("IIS://{0}/W3SVC/AppPools", Host));
// foreach (DirectoryEntry entry in appPools.Children)
// {
// if (entry.Name == appPoolName)
// {
// var manager = new ServerManager();
// app = manager.ApplicationPools[appPoolName];
// }
// }
// return app;
//}
///// <summary>
///// 判断程序池是否存在
///// </summary>
///// <param name="appPoolName">程序池名称</param>
///// <returns>true存在 false不存在</returns>
//public static bool IsAppPoolExsit(string appPoolName)
//{
// bool result = false;
// var appPools = new DirectoryEntry(string.Format("IIS://{0}/W3SVC/AppPools", Host));
// foreach (DirectoryEntry entry in appPools.Children)
// {
// if (entry.Name.Equals(appPoolName))
// {
// result = true;
// break;
// }
// }
// return result;
//}
///// <summary>
///// 删除指定程序池
///// </summary>
///// <param name="appPoolName">程序池名称</param>
///// <returns>true删除成功 false删除失败</returns>
//public static bool DeleteAppPool(string appPoolName)
//{
// bool result = false;
// var appPools = new DirectoryEntry(string.Format("IIS://{0}/W3SVC/AppPools", Host));
// foreach (DirectoryEntry entry in appPools.Children)
// {
// if (entry.Name.Equals(appPoolName))
// {
// try
// {
// entry.DeleteTree();
// result = true;
// break;
// }
// catch
// {
// result = false;
// }
// }
// }
// return result;
//}
///// <summary>
///// 创建应用程序池
///// </summary>
///// <param name="appPool"></param>
///// <returns></returns>
//public static bool CreateAppPool(string appPool)
//{
// try
// {
// if (!IsAppPoolExsit(appPool))
// {
// var appPools = new DirectoryEntry(string.Format("IIS://{0}/W3SVC/AppPools", Host));
// DirectoryEntry entry = appPools.Children.Add(appPool, "IIsApplicationPool");
// entry.CommitChanges();
// return true;
// }
// }
// catch
// {
// return false;
// }
// return false;
//}
///// <summary>
///// 编辑应用程序池
///// </summary>
///// <param name="application"></param>
///// <returns></returns>
//public static bool EditAppPool(ApplicationPool application)
//{
// try
// {
// if (IsAppPoolExsit(application.Name))
// {
// var manager = new ServerManager();
// manager.ApplicationPools[application.Name].ManagedRuntimeVersion = application.ManagedRuntimeVersion;
// manager.ApplicationPools[application.Name].ManagedPipelineMode = application.ManagedPipelineMode;
// //托管模式Integrated为集成 Classic为经典
// manager.CommitChanges();
// return true;
// }
// }
// catch
// {
// return false;
// }
// return false;
//}
//public static void getwebsitexml()
//{
//}
///// <summary>
///// 运行cmd命令
///// 会显示命令窗口
///// </summary>
///// <param name="cmdExe">指定应用程序的完整路径</param>
///// <param name="cmdStr">执行命令行参数</param>
//static bool RunCmd(string cmdExe, string cmdStr)
//{
// bool result = false;
// try
// {
// using (System.Diagnostics.Process myPro = new System.Diagnostics.Process())
// {
// //指定启动进程是调用的应用程序和命令行参数
// System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(cmdExe, cmdStr);
// myPro.StartInfo = psi;
// myPro.Start();
// myPro.WaitForExit();
// result = true;
// }
// }
// catch
// {
// }
// return result;
//}
///// <summary>
///// 运行cmd命令
///// 不显示命令窗口
///// </summary>
///// <param name="cmdStr">执行命令行参数</param>
//public static bool RunCmd2(string cmdStr)
//{
// bool result = false;
// try
// {
// using (System.Diagnostics.Process myPro = new System.Diagnostics.Process())
// {
// myPro.StartInfo.FileName = "cmd.exe";
// myPro.StartInfo.UseShellExecute = false;
// myPro.StartInfo.RedirectStandardInput = true;
// myPro.StartInfo.RedirectStandardOutput = true;
// myPro.StartInfo.RedirectStandardError = true;
// myPro.StartInfo.CreateNoWindow = true;
// myPro.Start();
// //如果调用程序路径中有空格时,cmd命令执行失败,可以用双引号括起来 ,在这里两个引号表示一个引号(转义)
// string str = string.Format(@"{0} {1}", cmdStr, "&exit");
// myPro.StandardInput.WriteLine(str);
// myPro.StandardInput.AutoFlush = true;
// myPro.WaitForExit();
// result = true;
// }
// }
// catch
// {
// }
// return result;
//}
///// <summary>
///// 运行cmd命令
///// 不显示命令窗口
///// </summary>
///// <param name="cmdExe">指定应用程序的完整路径</param>
///// <param name="cmdStr">执行命令行参数</param>
//public static bool RunCmd2(string cmdExe, string cmdStr)
//{
// bool result = false;
// try
// {
// using (System.Diagnostics.Process myPro = new System.Diagnostics.Process())
// {
// myPro.StartInfo.FileName = "cmd.exe";
// myPro.StartInfo.UseShellExecute = false;
// myPro.StartInfo.RedirectStandardInput = true;
// myPro.StartInfo.RedirectStandardOutput = true;
// myPro.StartInfo.RedirectStandardError = true;
// myPro.StartInfo.CreateNoWindow = true;
// myPro.Start();
// //如果调用程序路径中有空格时,cmd命令执行失败,可以用双引号括起来 ,在这里两个引号表示一个引号(转义)
// string str = string.Format(@"""{0}"" {1} {2}", cmdExe, cmdStr, "&exit");
// myPro.StandardInput.WriteLine(str);
// myPro.StandardInput.AutoFlush = true;
// myPro.WaitForExit();
// result = true;
// }
// }
// catch
// {
// }
// return result;
//}
//public static List<string> EnumWebSite()
//{
// DirectoryEntry de = new DirectoryEntry(string.Format("IIS://{0}/W3SVC/AppPools", Host));
// //DirectoryEntry de = new DirectoryEntry(strDePath);
// // PropertyValueCollection serverBindings = de.Properties["ServerBindings"];
// List<string> list = new List<string>();
// foreach (DirectoryEntry child in de.Children)
// {
// PropertyValueCollection serverBindings = child.Properties["ServerBindings"];
// if (child.Site != null)
// {
// Console.WriteLine("Name: {0}", child.Name);
// Console.WriteLine("SchemaClassName: {0}", child.Site.Name);
// Console.WriteLine("ServerBindings: {0}", child.Properties["ServerBindings"].Value);
// Console.WriteLine();
// //DirectoryEntry virEntry = new DirectoryEntry(child.Path + "/ROOT");
// }
// if (child.SchemaClassName == "IIsWebServer")
// {
// list.Add(child.Properties["ServerComment"].Value.ToString());
// }
// }
// return list;
//}
///// <summary>
///// 获取应用程序池->数组
///// </summary>
///// <returns></returns>
//public static string[] GetApplicationPools()
//{
// DirectoryEntry directoryEntry = new DirectoryEntry(string.Format("IIS://{0}/W3SVC/AppPools", Host));
// if (directoryEntry == null) return null;
// List<string> list = new List<string>();
// foreach (DirectoryEntry entry2 in directoryEntry.Children)
// {
// PropertyCollection properties = entry2.Properties;
// list.Add(entry2.Name.ToString().Trim());
// }
// return list.ToArray();
//}
///// <summary>
///// 获取程序池状态
///// </summary>
///// <param name="poolName"></param>
///// <returns></returns>
//public static ObjectState getAppPoolSatus(string poolName)
//{
// ServerManager manager = new ServerManager();
// return manager.ApplicationPools[poolName].State;
//}
//#region 回收、停止、启动
///// <summary>
///// 回收、停止、启动
///// </summary>
///// <param name="appPoolName"></param>
///// <param name="method">(Recycle)、停止(Stop)、启动(Start)</param>
///// <returns></returns>
//public static bool RSAppPool(string appPoolName, string method = "Recycle")
//{
// //如果应用程序池当前状态为停止,则会发生异常报错
// try
// {
// DirectoryEntry appPool = new DirectoryEntry(string.Format("IIS://{0}/W3SVC/AppPools", Host));
// DirectoryEntry findPool = appPool.Children.Find(appPoolName, "IIsApplicationPool");
// findPool.Invoke(method, null);
// appPool.CommitChanges();
// appPool.Close();
// return true;
// }
// catch
// {
// return false;
// }
//}
//#endregion
}
}