The first thing to understand before Windows services development, DOS
The physical path where the program resides
D:\workObject\xx.exe
Input cmd
CD C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
CD C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319
Installation Services
InstallUtil D:\workObject\xx.exe
Uninstall Service
Installutil/u D:\workObject\xx.exe
View the Window service
Services.msc
C:\>
To start a service by using the command line
There are two ways to open under CMD, net and sc,net are used to open services that are not disabled, and the syntax is:
NET start service name starts the net START service name
NET stop service name stops the net stop service name
To open a disabled service with SC, the syntax is:
sc Config Service name start= demand//Manual
SC condig Service name start= Auto//Auto
sc config service name start= disabled//disable
SC Start service Name
sc stop service Name
Here is the instance code
/// <summary> ///The main entry point for the application. /// </summary> Static voidMain (string[] args) {log4net. Config.XmlConfigurator.Configure (); //multiple User services can be run in the same process. To add//Another service is added to this process, change the downstream to//Create another service object. For example,// //ServicesToRun = new servicebase[] {new Service1 (), New MySecondUserService ()}; // if(args. Length = =0) {servicebase[] servicestorun; ServicesToRun=NewServicebase[] {NewRemovecacheservice ()};///New servicebase[] {new Service1 ()}; implementing Logical PortalsServicebase.run (ServicesToRun); } //Installation Services Else if(args[0]. ToLower () = =" /I"|| args[0]. ToLower () = ="- I.") { Try { string[] CmdLine = { }; stringServicefilename =System.Reflection.Assembly.GetExecutingAssembly (). Location; Transactedinstaller Transactedinstaller=NewTransactedinstaller (); Assemblyinstaller Assemblyinstaller=NewAssemblyinstaller (Servicefilename, cmdline); TRANSACTEDINSTALLER.INSTALLERS.ADD (Assemblyinstaller); Transactedinstaller.install (NewSystem.Collections.Hashtable ()); } Catch(Exception ex) {stringmsg =Ex. Message; } } //Delete Service Else if(args[0]. ToLower () = ="/ u"|| args[0]. ToLower () = ="- u") { Try { string[] CmdLine = { }; stringServicefilename =System.Reflection.Assembly.GetExecutingAssembly (). Location; Transactedinstaller Transactedinstaller=NewTransactedinstaller (); Assemblyinstaller Assemblyinstaller=NewAssemblyinstaller (Servicefilename, cmdline); TRANSACTEDINSTALLER.INSTALLERS.ADD (Assemblyinstaller); Transactedinstaller.uninstall (NULL); } Catch(Exception ex) {stringmsg =Ex. Message; } } }
C # Windows Service operations Instance