C # Windows Service operations Instance

Source: Internet
Author: User

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

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.