Click here to Skip to main content
Click here to Skip to main content

Window Services

, 17 Apr 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
How to create windows service

Introduction

This article will help you to create the simple window service which can be useful wile developing a windows application

Using the code

1) open new project select project type as C# --> WindowService templates ( in our case) you can choose VB as per your language preferance

2) This will add default service1.cs file with OnStart and OnStop()

3) choose the designer mode and give appropriate name for your service in property without any space and service name property as display name which can have spaces

4) add the new class (in our case MyWinServiceInstaller.cs) which will be installer class for the service

5) add the .net component reference of System.Configuration.Install Copy paste the code of MyWinServiceInstaller.cs file into your installer class
NB: you can add the displayname, servicename, starttype etc
you can also add userid and password using service process ( inour case its null)

                   
[RunInstallerAttribute(true)]
public class MyWinServiceInstaller : System.Configuration.Install.Installer
{
    public MyWinServiceInstaller()
    {
        ServiceInstaller si = new ServiceInstaller();
        ServiceProcessInstaller spi = new ServiceProcessInstaller();
        si.ServiceName = "MyWinService";
        si.DisplayName = "My Windows Service";
        si.StartType = ServiceStartMode.Automatic;
        this.Installers.Add(si);
        spi.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
        spi.Username = null;
        spi.Password = null;
        this.Installers.Add(spi); 
    }

}

Your service is now ready to run
1)build your solution
2)locate the exe file of your service
3)go to command prompt and locate the following installutil.exe in

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 (for .net framework 2.0)
C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322 (for .net framework 1.1)
C:\WINDOWS\Microsoft.NET\Framework\v1.0.3707 (for .net framework 1.0)

or you can use following command

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe c:\samplewinservice\samplewinservice\bin\Release\samplewinservice.exe
for installation 

and

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe c:\samplewinservice\samplewinservice\bin\Release\samplewinservice.exe -u
for uninstallation.

you can then go to service management console to view the service installed you have to start the service manually even if it is set as automatic
but it will get start automatically on system start up....

you can verify current sample service working by checking the event log after the service is started

Enjoy servicing........

A trick or tips.

can be use to keep watch on perticular folder and can be used for keepting track of various activity going on records

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

TangoCharli (Anant Tiwari)
Software Developer (Senior) Avenues Technologies Pvt Ltd
India India
MCTS Microsoft Certified Technology Specialist
C-DAC
Computer Engineer

Comments and Discussions

 
Generalgood article PinmemberDonsw17-Jan-09 18:42 
GeneralQuick Overview PinmemberBruce Ricker19-May-08 2:27 
GeneralNothing New Here PinmvpJohn Simmons / outlaw programmer17-Apr-08 7:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141216.1 | Last Updated 17 Apr 2008
Article Copyright 2008 by TangoCharli (Anant Tiwari)
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid