C # control IIS startup, stop, and restart
Code
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"); // restart
21 break;
22 case servicecontrollerstatus. startpending:
23 break;
24 case servicecontrollerstatus. stoppending:
25 break;
26 case servicecontrollerstatus. Stopped:
27 SC. Start (); // start
28 break;
29 default:
30 break;
31}
32 SC. Close ();
33 SC. Dispose ();
34