C#windows Service Program creation installation and uninstallation one, the development environment
Operating system: WINDOWS7X64 SP1 Professional Edition
Development environment: Visual Studio 2013
Programming Language: C #
. NET version:. NET Frmework 4.0
Ii. Creating a Windows service Program
1. Create a new Windows service program named Servicedemo
2. Modify the Service1.cs class to Servicedemo
3. By default, you can see that the following code overrides a OnStart () and OnStop () method
1 protected Override void OnStart (string[] args)2 {3 }4 5 protected Override void OnStop () 6 {7 }
4. Add a few lines of test code in Start (OnStart) and stop (OnStop)
1 /// <summary>2 ///Start3 /// </summary>4 /// <param name= "args" ></param>5 protected Override voidOnStart (string[] args)6 {7 stringStartstr =string. Format ("{0}-{1}", DateTime.Now.ToString ("YYYYMMDDHHMMSS"),"Program Startup");8 Log (STARTSTR);9 }Ten One /// <summary> A ///Stop - /// </summary> - protected Override voidOnStop () the { - stringStopstr =string. Format ("{0}-{1}", DateTime.Now.ToString ("YYYYMMDDHHMMSS"),"Program Stop"); - Log (STOPSTR); - } + voidLog (stringstr) - { + stringPath ="D://logfile.log"; A using(StreamWriter SW =file.appendtext (path)) at { - SW. WriteLine (str); - } -}
5. Double-click Servicedemo to enter the Servicedemo design interface in the blank position right-click menu to add the installer, as shown in
6. You can see that two components appear in the design interface
7. Modify these two components section properties
The ServiceInstaller1 Properties window changes servicename to servicedemo,description to service Demo Program,starttype to Manual
Modify the ServiceProcessInstaller1 property to change the account to LocalSystem as shown
8. The right mouse button compilation generator can see the specific directory of the generator program
9. Here a Windows service program is written.
Third, install the service program
1. Run command terminal as Administrator (remember as Administrator)
Open Command Terminal program
2. Installation Services
Installutil.exe filename
Uninstall Service:
installutil.exe/u filename
Install the service program
Because the Installutil.exe program is in the C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ directory, you need to switch directories through the cmd command "CD". v4.0.30319 is the version of the Windows service program that was compiled (choose the corresponding version yourself)
The input is as follows: (choose according to the path of your own program, enter the completed carriage return)
Installation Completion Tips
After the installation is complete, you can see the service program that you just wrote in the list of services, managed by computer
You can manually right-click Start \ Stop Program
Uninstall Service
You only need to add/u behind installutil.exe to uninstall the command
Depending on the function of the previous program start or stop there will be a log record on the D drive to see the following
C#windows Service Program creation, installation and uninstallation