How To Create a Window Service in C#.net?
Want to learn how to create a window service in C#? Vizteck is giving you a step-by-step detailed guide for it.
May 1, 2015
Window Service creates executable applications that run in its Window Session. Depending upon the configuration setting, they can start manually or automatically when the computer starts. I’ll explain step by step how to create a Window Service in C#.Net.
- Click File -> New Project.
- Select Window Service Template.
- Name the service MyWindowService.
- Rename the Service1.cs to MyService.cs.
- Add Event log to the Service, by clicking tool and dropingEventLog.
- Rename the eventLog1 to EventLog4MyService.'
- Right, Click the MyService.cs and select View designer.
- In the designer window right click and click Add “Add Installer”.
- Select serviceProcessInstaller1 and in the property window, adjust the fields as in the below figure.
- Right-click MyService and select code. Type the following code.
- Right Click the MyWindowService project and select properties.
- Select the Target Framework. (in my case it is 4.5)
- Select targetplateform (in my case x64).
- Type PostBuild Events Commands
- This command will uninstall
- This command will install MyWindowService.exe.
- Now Build the project. If the build is successful, the service will also be installed.
Create a file in the notepad, and type the following script
Save the file as “install.bat”.To uninstall the service, use the following script in another Uninstall.bat file.
Running the file as administrator will install the service. Type services. MSC and go to services and run the service.
(Window Service “MyService” has successfully installed)