C # perform operations on the wiindows Service

Source: Internet
Author: User
1 using system; 2 using system. collections. generic; 3 using system. LINQ; 4 using system. text; 5 using system. threading. tasks; 6 using system. collections. generic; 7 using system. windows; 8 using Microsoft. win32; 9 using system. security; 10 using system. diagnostics; 11 using system. serviceprocess; 12 using system. threading; 13 using system. configuration. install; 14 15 16 17 18 namespace updatemodule 19 {20 public class detectservice 21 {22/* 23 * First add system. serviceprocess. DLL reference 24*25 */26 // If the Windows service is stopped, start Windows Service 27 28/static void main (string [] ARGs) 29 // {30 /// stopwindowsservice ("smservice"); 31 // startwindowsservice ("smservice"); 32 // uninstallservice (); 33 // process. start ("net stop smservice"); 34 // string STR = console. readline (); 35 36 37 //} 38 // enable Windows Service 39 public static int startwindowsservice (string servicename) 40 {41 servicecontroller [] SCS = servicecontroller. getservices (); 42 // 0 indicates that the instance is not started. 43 int bresult = 0; 44 foreach (servicecontroller SC in SCS) 45 {46 47 If (SC. displayname. contains (servicename) & SC. status = servicecontrollerstatus. stopped) 48 {49 try 50 {51 SC. waitforstatus (servicecontrollerstatus. stopped, timespan. fromseconds (30); 52 SC. start (); 53 // Let the thread sleep for 1 second. Wait for loading of other services. If this parameter is not set, 54 threads will fail. sleep (1000); 55 // 1 indicates the startup status 56 bresult = 1; 57} 58 catch (exception ex) 59 {60 // 2 indicates the startup failure 61 bresult = 2; 62 throw ex; 63} 64} // C # Start Windows and close 65} 66 return bresult; 67} 68 69 // stop Windows Service 70 public static bool stopwindowsservice (string servicename) 71 {72 servicecontroller [] SCS = servicecontroller. getservices (); 73 bool bresult = false; 74 foreach (servicecontroller SC in SCS) 75 {76 if (SC. displayname. contains (servicename) 77 {78 try 79 {80 SC. waitforstatus (servicecontrollerstatus. running, timespan. fromseconds (30); 81 SC. stop (); 82 bresult = true; 83} 84 catch (exception ex) 85 {86 bresult = false; 87 throw ex; 88} 89} 90} 91 return bresult; 92} 93 94 // uninstall Windows Service 95 public static void uninstallservice () 96 {97 98 system. diagnostics. PROCESS p = new system. diagnostics. process (); 99 p. startinfo. filename = "cmd.exe"; 100 P. startinfo. useshellexecute = false; // whether to start 101 p using the operating system shell. startinfo. redirectstandardinput = true; // accept the input information from the caller 102 p. startinfo. redirectstandardoutput = true; // the output information is obtained by the calling program 103 p. startinfo. redirectstandarderror = true; // redirected standard error output 104 p. startinfo. createnowindow = true; // The program window 105 P is not displayed. start (); // Start Program 106 107 // send the input information 108 p to the CMD window. standardinput. writeline (@ "SC Delete smservice" + "& Exit"); 109 // P. standardinput. autoflush = true; 110 // obtain the output information of the CMD window. 111 // string output = P. standardoutput. readtoend (); 112 p. waitforexit (); // wait for the program to run and exit the process 113 p. close (); 114 115 116 // console. writeline (output); 117 // console. readkey (); 118 119} 120 121} 122}

 

C # perform operations on the wiindows Service

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.