Click here to Skip to main content
Licence CPOL
First Posted 17 Apr 2008
Views 10,438
Downloads 78
Bookmarked 16 times

Window Services

By | 17 Apr 2008 | Article
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)

About the Author

TangoCharli (Anant Tiwari)

Software Developer (Senior)

India India

Member

MCTS Microsoft Certified Technology Specialist
C-Dac
Computer Engineer

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalgood article PinmemberDonsw17:42 17 Jan '09  
GeneralQuick Overview PinmemberBruce Ricker1:27 19 May '08  
GeneralNothing New Here PinmvpJohn Simmons / outlaw programmer6:12 17 Apr '08  

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.

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