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.

image

Technology

image

May 1, 2015

image

Babar

image

 

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.'

 

html12.PNG

 

  • 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.

 

html11.PNG

  • Right-click MyService and select code. Type the following code.

 

html 10.PNG

html 9.PNG

How To Build and Deploy the Service

 

  • Right Click the MyWindowService project and select properties.
  • Select the Target Framework. (in my case it is 4.5)

html 8.PNG

 

  • Select targetplateform (in my case x64).

html7.PNG

  • Type PostBuild Events Commands

 

html6.PNG

  • This command will uninstall

html5.PNG
 

  • This command will install MyWindowService.exe.

hml4.PNG

  • Now Build the project. If the build is successful, the service will also be installed.

html 3.PNG

 

Create a file in the notepad, and type the following script

html2.PNGSave the file as “install.bat”.To uninstall the service, use the following script in another Uninstall.bat file.

html.PNG

 

Running the file as administrator will install the service. Type services. MSC and go to services and run the service.

 

05c70c_f4502e533c8740c39694d81d646e544c_mv2.webp
 

(Window Service “MyService” has successfully installed)

 

Islamabad, Pakistan

Floor 2, Building 145, Civic Center Bahria Town Phase 4, Islamabad, Pakistan

USA

1 squire armor road, windham, NH,03076

Saudi Arabia

7654 Prince Nawaf Street, Al Khobar, 34426

Kingdom Of Bahrain

No 851, Bldg No 2420, Rd No 2831, Seef District, Block 428