Using C to write a service running secretly in the background
Midnight 2020-11-26 10:00:26


 use C# Write a service that runs secretly in the background

It's just a routine


In the last article 《 There's an unknown program called service 》 in , We've done a little bit of that Windows service .

Today we use C# To write a Windows The backstage service . Finish writing the service , compile , Installation and start up , Run through Windows The basic pattern of service development .

Write a service


We use VS 2019, To write Services :


 use C# Write a service that runs secretly in the background

open VS-> Create project



 use C# Write a service that runs secretly in the background

Find a service -> next step



 use C# Write a service that runs secretly in the background

Enter the basic information of the service -> establish



 use C# Write a service that runs secretly in the background

List of files created



 use C# Write a service that runs secretly in the background

Rename the service to MyService1



 use C# Write a service that runs secretly in the background

Add installation module for service


You must add an installer for the service , And configure the name of the service and other basic information , The service can be installed normally .


 use C# Write a service that runs secretly in the background

Add the file list after installing the module



 use C# Write a service that runs secretly in the background

Modify the service type



 use C# Write a service that runs secretly in the background

Modify the relevant service type


We use... For the time being LocalService, Later, we will explain other types of services , And the difference between different services .

 use C# Write a service that runs secretly in the background

Modify the relevant information



 use C# Write a service that runs secretly in the background

Modify the relevant information


Compile Services




 use C# Write a service that runs secretly in the background

Right click -> Generate


Right click on our project , Click generate

Install and start services


Installation services , We need to use an installation tool :installutil. This tool is in VS2019 Is already installed by default .

We are VS2019 Open in Power Shell:


 use C# Write a service that runs secretly in the background


Get into Debug Catalog :


 use C# Write a service that runs secretly in the background


cd .\TestService\bin\Debug\

Then execute the install command :

installutil .\TestService.exe

 use C# Write a service that runs secretly in the background

Installation successful , And start the service



 use C# Write a service that runs secretly in the background

Installation successful , And start the service


 use C# Write a service that runs secretly in the background

Service started successfully

Service offload


Uninstall command , On the basis of the installation command , Add parameters /u that will do , The order is :

installutil .\TestService.exe /u

 use C# Write a service that runs secretly in the background

Uninstall service


After the service is successfully unloaded , In the list of services , We won't see our service anymore .

At the same time, when unloading the service , Will automatically stop service .

Thank you for seeing !

If you follow me step by step here , So congratulations , You're done Windows The basic pattern of service development .

Next up


Advanced service :

How to execute business code in a service ?

How to debug the service ?

How the service supports pauses / recovery ?

And advanced development secrets for other services !

Coming soon ...

Next article 《 Perform silently in service C# Business code 》, Restore review !
版权声明
本文为[Midnight]所创,转载请带上原文链接,感谢
https://fheadline.com/2020/11/20201126094836691n.html
相似文章

2021-08-09