Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create Windows Services in C #

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to create Windows services in C#, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

C # create Windows service (Windows Services)

The Windows service was called the NT service in previous versions of Visual Studio, and the new name is enabled in VS.net. Creating a Windows service with Visual C # is not a difficult task, so this article will guide you through creating a Windows service step by step and using it. When the service starts and stops, it writes some text information to a text file.

C # create Windows service * step: create a service framework

To create a new Windows service, select the Windows Service (Windows Service) option from the Visual C# project, give the project a new file name, and click OK.

As you can see, the wizard adds the WebService1.cs class to the project file:

The meanings of each attribute are:

◆ Autolog

Whether to automatically write to the log file of the system

◆ CanHandlePowerEvent

Accept power events during service

◆ CanPauseAndContinue

Whether the service accepts requests to suspend or resume running

◆ CanShutdown

Whether the service is notified when the computer running it shuts down so that the OnShutDown procedure can be called

◆ CanStop

Whether the service accepts the request to stop running

◆ ServiceName

Service name

C # create Windows service step 2: add functionality to the service

In the .cs code file, we can see that there are two ignored functions OnStart and OnStop.

The OnStart function is executed when the service is started, and the OnStop function is executed when the service is stopped. Here, when you start and stop the service, write some text information to a text file, as follows:

Protected override void OnStart (string [] args) {FileStream fs = new FileStream (@ "d:\ mcWindowsService.txt", FileMode.OpenOrCreate, FileAccess.Write); StreamWriter m_streamWriter = new StreamWriter (fs); m_streamWriter.BaseStream.Seek (0, SeekOrigin.End); m_streamWriter.WriteLine ("mcWindowsService: Service Started" + DateTime.Now.ToString () + "\ n"); m_streamWriter.Flush (); m_streamWriter.Close (); fs.Close () } protected override void OnStop () {FileStream fs = new FileStream (@ "d:\ mcWindowsService.txt", FileMode.OpenOrCreate, FileAccess.Write); StreamWriter m_streamWriter = new StreamWriter (fs); m_streamWriter.BaseStream.Seek (0, SeekOrigin.End); m_streamWriter.WriteLine ("mcWindowsService: Service Stopped" + DateTime.Now.ToString () + "\ n"); m_streamWriter.Flush (); m_streamWriter.Close (); fs.Close ();}

C # create Windows service step 3: add the installer to the service application

Visual Studio.NET comes with installation components that can be used to install resources associated with service applications. The installation component registers a single service on the system to which it is being installed and lets the service control manager know that the service exists.

To install the service correctly, you do not need any special coding in the installer. However, if you need to add special features to the installation process, you may need to modify the contents of the installer occasionally.

The steps to add the installer to the service application are:

1: in the solution, access the Design view of the service to which you want to add the installed component.

2: in the properties window, click the add installer link

At this point, a new class ProjectInstaller and two installation components ServiceProcessInstaller and ServiceInstaller are added to the project, and the property values of the service are copied to the component.

3: to determine how to start the service, click the ServiceInstaller component and set the StartType property to the appropriate value.

◆ Manual

After the service is installed, it must be started manually.

◆ Automatic

The service starts automatically each time the computer restarts.

◆ Disabled

The service cannot be started.

4: change the Account property of the serviceProcessInstaller class to LocalSystem

In this way, no matter which user you log in to the system, the service will always start.

C # create Windows service step 4: generate service program

Build the project by selecting build from the build menu.

Be careful not to run the project by pressing the F5 key-you cannot run the service project in this way.

C # create Windows service step 5: install the service

Access the directory where the compiled executable is located in the project.

Run InstallUtil.exe from the command line with the output of the project as an argument. Enter the following code on the command line:

Installutil yourproject.exe

Uninstall the service

Run InstallUtil.exe from the command line with the output of the project as an argument.

Installutil / u yourproject.exe above are all the contents of the article "how to create Windows Services in C #". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report