Click here to Skip to main content
15,870,130 members
Articles / Programming Languages / C#
Article

Window Services

Rate me:
Please Sign up or sign in to vote.
1.62/5 (10 votes)
17 Apr 2008CPOL2 min read 25.5K   238   16   3
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)


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

Comments and Discussions

 
Generalgood article Pin
Donsw17-Jan-09 17:42
Donsw17-Jan-09 17:42 
GeneralQuick Overview Pin
Bruce Ricker19-May-08 1:27
Bruce Ricker19-May-08 1:27 
GeneralNothing New Here Pin
#realJSOP17-Apr-08 6:12
mve#realJSOP17-Apr-08 6:12 

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

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