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