I. C # Use processstartinfo to install the service, uninstall the service, enable the Service, and disable the service!
C # Use processstartinfo to install the service, uninstall the service, enable the Service, and disable the service! <Br/> /// <summary> <br/> // enable the service from the CMD running <br/> /// </Summary> <br/> // /<Param name = "sender"> </param> <br/> // <Param name = "E"> </param> <br/> protected void button4_click1 (object sender, eventargs e) <br/>{< br/> // enable the Service <br/> processstartinfo A = new processstartinfo (@ "C:/Windows/system32/cmd.exe ", "/C Net start service name"); <br/>. windowstyle = processwindowstyle. hidden; <br/> Process = process. start (a); </P> <p >}< br/> protected void button5_click1 (Object sender, eventargs E) <br/> {<br/> // close the service <br/> processstartinfo A = new processstartinfo (@ "C:/Windows/system32/cmd.exe ", "/C net stop service name"); <br/>. windowstyle = processwindowstyle. hidden; <br/> Process = process. start (a); </P> <p >}< br/> protected void button6_click1 (Object sender, eventargs E) <br/>{ <br/> processstartinfo A = new processstartinfo (@ "D: // zhengxinle // xiangmu // netview // transmit.exe ", "-install"); <br/> console. write ("successfully installed the service"); <br/>. windowstyle = processwindowstyle. hidden; <br/> Process = process. start (a); <br/>}</P> <p> protected void button7_click1 (Object sender, eventargs E) <br/>{ <br/> processstartinfo A = new processstartinfo (@ "D: // zhengxinle // xiangmu // netview // transmit.exe ", "-Remove"); <br/> console. write ("successfully uninstalled"); <br/>. windowstyle = processwindowstyle. hidden; <br/> Process = process. start (a); <br/>}< br/>
Ii. load through API functions
Using system. configuration. install; <br/> using system. serviceprocess; <br/> using system. runtime. interopservices; </P> <p> # region dllimport </P> <p> [dllimport ("advapi32.dll")] <br/> Public static extern intptr openscmanager (string lpmachinename, string lpscdb, int scparameter); <br/> [dllimport ("advapi32.dll")] <br/> Public static extern intptr createservice (intptr SC _handle, string lpsvcname, string LPD Isplayname, <br/> int dwdesiredaccess, int dwservicetype, int dwstarttype, int dwerrorcontrol, string lppathname, <br/> string lploadordergroup, int lpdwtagid, string lpdependencies, string lpservicestartname, string lppassword); <br/> [dllimport ("advapi32.dll")] <br/> Public static extern void closeservicehandle (intptr schandle ); <br/> [dllimport ("advapi32.dll")] <br/> Public static extern int startser Vice (intptr svhandle, int dwnumserviceargs, string lpserviceargvectors); <br/> [dllimport ("advapi32.dll", setlasterror = true)] <br/> Public static extern intptr openservice (intptr schandle, string lpsvcname, int dwnumserviceargs); <br/> [dllimport ("advapi32.dll")] <br/> Public static extern int deleteservice (intptr svhandle); <br/> [dllimport ("kernel32.dll")] <br/> Public static extern int getlasterror (); </P> <p> # endregion dllimport </P> <p> // <br/> // install and run <br/> ////// C # installation path. <br/> // service name <br/> //// service display name. <br/> ///// check whether the service is successfully installed. <br/> Public bool installservice (string svcpath, string svcname, string svcdispname) <br/>{< br/> # region constants declaration. <br/> int SC _manager_create_service = 0x0002; <br/> int service_win32_own_process = 0x00000010; <br/> // int service_demand _ Start = 0x00000003; <br/> int service_error_normal = 0x00000001; <br/> int standard_rights_required = 0xf0000; <br/> int service_query_config = 0x0001; <br/> int service_change_config = 0x0002; <br/> int service_query_status = 0x0004; <br/> int service_enumerate_dependents = 0x0008; <br/> int service_start = 0x0010; <br/> int service_stop = 0x0020; <br/> int service_pause_continue = 0x0040; <br/> int Service_interrogate = 0x0080; <br/> int service_user_defined_control = 0x0100; <br/> int service_all_access = (optional | <br/> service_query_config | <br/> service_change_config | <br/> service_query_status | <br/> service_enumerate_dependents | <br/> service_start | <br/> service_stop | <br/> service_pause_continue | <br/> service_interrogate | <br/> service_user_defined_control ); <Br/> int service_auto_start = 0x00000002; <br/> # endregion constants declaration. <br/> try {<br/> intptr SC _handle = openscmanager (null, null, SC _manager_create_service); <br/> If (SC _handle.toint32 ()! = 0) <br/>{< br/> intptr sv_handle = createservice (SC _handle, svcname, svcdispname, service_all_access, role, service_auto_start, role, svcpath, null, 0, null, null, null); <br/> If (sv_handle.toint32 () = 0) <br/>{< br/> closeservicehandle (SC _handle); <br/> return false; <br/>}< br/> else <br/> {<br/> // try to start the service <br/> int I = startservice (sv_handle, 0, null); <br/> if (I = 0) <Br/>{< br/> return false; <br/>}< br/> closeservicehandle (SC _handle); <br/> return true; <br/>}< br/> else <br/> return false; <br/>}< br/> catch (exception E) </P> <p >{< br/> throw E; <br/>}< br/> // <br/> // reinstall the service. <br/> // service name. <br/> Public bool uninstallservice (string svcname) <br/>{< br/> int generic_write = 0x40000000; <br/> intptr SC _hndl = openscmana GER (null, null, generic_write); <br/> If (SC _hndl.toint32 ()! = 0) <br/> {int Delete = 0x10000; <br/> intptr svc_hndl = openservice (SC _hndl, svcname, delete ); <br/> If (svc_hndl.toint32 ()! = 0) <br/>{< br/> int I = deleteservice (svc_hndl); <br/> if (I! = 0) <br/>{< br/> closeservicehandle (SC _hndl); <br/> return true; <br/>}< br/> else <br/>{< br/> closeservicehandle (SC _hndl); <br/> return false; <br/>}< br/> else <br/> return false; <br/>}
3. Load an assembly and run all the installation programs.
/// // @ Is the service name <br/> /// <summary> <br/> // use servicecontroller to enable the Service <br/> /// </Summary> <br/> /// <Param name = "sender"> </param> <br/> /// <Param name = "e"> </param> <br/> protected void button5_click (Object sender, eventargs e) <br/>{< br/> system. serviceprocess. servicecontroller mycontroller = <br/> new system. serviceprocess. servicecontroller ("@"); </P> <p> // The continuepending service is about to continue. This corresponds to the Win32 service_continue_pending constant, which is defined as 0x00000005. <Br/> // The paused service has been suspended. This corresponds to the Win32 service_paused constant, which is defined as 0x00000007. <Br/> // The pausepending service is about to be suspended. This corresponds to the Win32 service_pause_pending constant, which is defined as 0x00000006. <Br/> // The running service is running. This corresponds to the Win32 service_running constant, which is defined as 0x00000004. <Br/> // The startpending service is starting. This corresponds to the Win32 service_start_pending constant, which is defined as 0x00000002. <Br/> // The stopped service is not running. This corresponds to the Win32 service_stopped constant, which is defined as 0x00000001. <Br/> // The stoppending service is stopping. This corresponds to the Win32 service_stop_pending constant, which is defined as 0x00000003. </P> <p> If (mycontroller. status. equals (servicecontrollerstatus. stopped) | mycontroller. status. equals (servicecontrollerstatus. stoppending) <br/>{< br/> mycontroller. start (); <br/> This. button5.text = "Close service"; <br/> response. write ("<MCE: script language = JavaScript> <! -- <Br/> alert ('enabled'); <br/> // --> </MCE: SCRIPT> "); <br/>}< br/> else <br/>{< br/> mycontroller. stop (); <br/> This. button5.text = "enable service"; <br/> response. write ("<MCE: script language = JavaScript> <! -- <Br/> alert ('close Service'); <br/> // --> </MCE: SCRIPT> "); <br/>}</P> <p> 1. Installation Service: </P> <p> private void installservice (idictionary statesaver, string filepath) </P> <p >{</P> <p> try </P> <p >{</P> <p> system. serviceprocess. servicecontroller service = new system. serviceprocess. servicecontroller ("servicename"); </P> <p> If (! Serviceisexisted ("servicename ")) </P> <p >{</P> <p> // install service </P> <p> assemblyinstaller myassemblyinstaller = new assemblyinstaller (); </P> <p> myassemblyinstaller. usenewcontext = true; </P> <p> myassemblyinstaller. path = filepath; </P> <p> myassemblyinstaller. install (statesaver); </P> <p> myassemblyinstaller. commit (statesaver); </P> <p> myassemblyinstaller. dispose (); </P> <p> // -- start service </P> <p> servi CE. start (); </P> <p >}</P> <p> else </P> <p >{</P> <p> If (service. status! = System. serviceprocess. servicecontrollerstatus. Running & service. status! = System. serviceprocess. servicecontrollerstatus. startpending) </P> <p >{</P> <p> service. start (); </P> <p >}</P> <p> catch (exception ex) </P> <p >{</P> <p> throw new exception ("installserviceerror/N" + ex. message); </P> <p >}</P> <p> 2. Uninstall the Windows Service: </P> <p> private void uninstallservice (string filepath) </P> <p >{</P> <p> try </P> <p >{</P> <p> If (serviceisexisted ("servicename ")) </P> <p> {</ P> <p> // uninstall service </P> <p> assemblyinstaller myassemblyinstaller = new assemblyinstaller (); </P> <p> myassemblyinstaller. usenewcontext = true; </P> <p> myassemblyinstaller. path = filepath; </P> <p> myassemblyinstaller. uninstall (null); </P> <p> myassemblyinstaller. dispose (); </P> <p >}</P> <p> catch (exception ex) </P> <p >{</P> <p> throw new exception ("uninstallserviceerror/N" + ex. message); </P> <P >}</P> <p> 3. Determine whether the window service exists: </P> <p> private bool serviceisexisted (string servicename) </P> <p >{</P> <p> servicecontroller [] services = servicecontroller. getservices (); </P> <p> foreach (servicecontroller s in services) </P> <p >{</P> <p> If (S. servicename = servicename) </P> <p >{</P> <p> return true; </P> <p >}</P> <p> return false; </P> <p >}</P> <p> 4. Start the service: </P> <p> private void startservice (Str Ing servicename) </P> <p >{</P> <p> If (serviceisexisted (servicename )) </P> <p >{</P> <p> system. serviceprocess. servicecontroller service = new system. serviceprocess. servicecontroller (servicename); </P> <p> If (service. status! = System. serviceprocess. servicecontrollerstatus. Running & service. status! = System. serviceprocess. servicecontrollerstatus. startpending) </P> <p >{</P> <p> service. start (); </P> <p> for (INT I = 0; I <60; I ++) </P> <p >{</P> <p> service. refresh (); </P> <p> system. threading. thread. sleep (1000); </P> <p> If (service. status = system. serviceprocess. servicecontrollerstatus. running) </P> <p >{</P> <p> break; </P> <p >}</P> <p> if (I = 59) </P> <p >{</P> <p> throw new exception (startserviceerror. replace ("$ S $", servicename )); </P> <p >}</P> <p>} </P> <p> 5. Stop the service: </P> <p> private void stopservice (string servicename) </P> <p >{</P> <p> If (serviceisexisted (servicename )) </P> <p >{</P> <p> system. serviceprocess. servicecontroller service = new system. serviceprocess. servicecontroller (servicename); </P> <p> If (service. status = system. serviceprocess. servicecontrollerstatus. running) </P> <p >{</P> <p> service. stop (); </P> <p> for (INT I = 0; I <60; I ++) </P> <p >{</P> <p> service. refresh (); </P> <p> system. threading. thread. sleep (1000); </P> <p> If (service. status = system. serviceprocess. servicecontrollerstatus. stopped) </P> <p >{</P> <p> break; </P> <p >}</P> <p> if (I = 59) </P> <p >{</P> <p> throw new exception (stopserviceerror. replace ("$ S $", servicename )); </P> <p >}</P> <p>} </P> <p> note: manually install the window service: </P> <p> in the "Visual Studio 2005 command prompt" window, run: </P> <p> install the service: installutil servicepath </P> <p> dismounting service: installutil/u servicepath