控制IIS启动,停止,重启(转)
C#控制IIS启动,停止,重启
代码
1 using System.Diagnostics;
2 using System.ServiceProcess;
3
4
5
6 // Reference System.ServiceProcess And System.Diagnostics namespace
7 ServiceController sc = new ServiceController("iisadmin");
8 switch (sc.Status)
9 {
10 case ServiceControllerStatus.ContinuePending:
11 break;
12 case ServiceControllerStatus.PausePending:
13 break;
14 case ServiceControllerStatus.Paused:
15 sc.Continue();
16 break;
17 case ServiceControllerStatus.Running:
18 //sc.Stop();
19 //sc.Start();
20 Process.Start("iisadmin");//重启
21 break;
22 case ServiceControllerStatus.StartPending:
23 break;
24 case ServiceControllerStatus.StopPending:
25 break;
26 case ServiceControllerStatus.Stopped:
27 sc.Start(); //启动
28 break;
29 default:
30 break;
31 }
32 sc.Close();
33 sc.Dispose();
34
2 using System.ServiceProcess;
3
4
5
6 // Reference System.ServiceProcess And System.Diagnostics namespace
7 ServiceController sc = new ServiceController("iisadmin");
8 switch (sc.Status)
9 {
10 case ServiceControllerStatus.ContinuePending:
11 break;
12 case ServiceControllerStatus.PausePending:
13 break;
14 case ServiceControllerStatus.Paused:
15 sc.Continue();
16 break;
17 case ServiceControllerStatus.Running:
18 //sc.Stop();
19 //sc.Start();
20 Process.Start("iisadmin");//重启
21 break;
22 case ServiceControllerStatus.StartPending:
23 break;
24 case ServiceControllerStatus.StopPending:
25 break;
26 case ServiceControllerStatus.Stopped:
27 sc.Start(); //启动
28 break;
29 default:
30 break;
31 }
32 sc.Close();
33 sc.Dispose();
34