Initial knowledge of Windows services

Source: Internet
Author: User

1. Create a new Windows service

2. Switch to Code view and copy the following code into

The service creates a d:/1.txt file at 10S intervals

Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.diagnostics;using system.io;using system.linq;using system.serviceprocess;using System.Text;using System.threading.tasks;using System.Timers; namespace windowsservicetest{public partial class Service1:servicebase{public Service1 () {InitializeComponent ();} protected override void OnStart (string[] args) {//Service Open Execution Code startdosomething ();} protected override void OnStop () {//service end execution code}protected override void OnPause () {//service suspend execution code base. OnPause ();} protected override void OnContinue () {//Service recovery execution code base. OnContinue ();} protected override void OnShutdown () {//system will close execution code base. OnShutdown ();} private void Startdosomething () {System.Timers.Timer timer = new System.Timers.Timer (10000);//interval 10 seconds timer. AutoReset = True;timer. Enabled = false; Executes the timer once. Elapsed + = new Elapsedeventhandler (writesomething); timer. Start ();} private void Writesomething (object source, System.Timers.ElapsedEventArgs e) {FileStream fs = Null;trY{fs = new FileStream ("D:/1.txt", FileMode.OpenOrCreate); string strText = @ "Creates the file repeatedly at 10-second intervals and remains unchanged if there is a file with the same name."// Get byte array byte[] data = new UTF8Encoding (). GetBytes (StrText);//start writing to FS. Write (data, 0, data. LENGTH);//clear buffer, close stream fs. Flush (); fs. Close (); fs. Dispose ();} CATCH{}FINALLY{IF (fs! = NULL) {fs. Close (); fs. Dispose ();}}}}

  

3. Adding the installer and setting the control properties 1. Right-click on the design page and select Add Installer

2. Set the Account property of the first control in the upper-left corner to LocalService

3. You can modify the servicename of the second control (service name, non-existing conflict with the system), StartType set to Automatic

4. Compiling the project

1. Build the solution (CTRL+SHIFT+B) and generate the corresponding Xxx.exe when the compilation is complete

2. Find the installation directory of the InstallUtil inside the system such as C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe can't find it, use everything.

3.win+r CMD CD jump to InstallUtil installation path, run the following command installutil.exe+ space +4.1 generated EXE directory

CD C:\Windows\Microsoft.NET\Framework64\v4.0.30319

InstallUtil.exe E:\GitVSTest\WindowsServiceTest\WindowsServiceTest\bin\Debug\WindowsServiceTest.exe

5. Start the service In the computer-administration-services and applications-services, find the service you just compiled, right-click to start6. Modify the service 1. In the service, stop the corresponding service 2. Modify the source code and build the solution again (CTRL+SHIFT+B) 3. Start the service again7. Uninstall the service The name of the uninstall service run is similar to the 4.3 still in the InstallUtil directory, but the directory that runs installutil.exe/u generated EXE, for example: C:\Windows\Microsoft.NET\Framework64\ V4.0.30319>installutil.exe/u E:\GitVSTest\WindowsServiceT2\WindowsServiceT2\bin\Debug\WindowsServiceT2.exe

Initial knowledge of Windows services

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.