Wednesday, October 29, 2008

How to create a simple Windows Service in C#..

With due permission from Saji P Babu i'm writing this topic in my blog

Windows Services are programs that can be started when the OS starts.... This will be running in the background of the Windows.

In this i'm just creating a simple windows service. For theory parts just refer some other sites...

Here is the practical part.

First we are going to create a Windows Service. For that
1) Open visual studio,
2) Take File menu -->New-->Project
3) From the list take a Windows service Application

Now you'll be getting a form without a design with name "projectIntaler.cs"

Next step is
  • Right Click on the form and select AddInstaller
  • Now you will get two Controls on the form
    • ProjectInstaller1
    • serviceInstaller1
Take the Properties of projectInstaller1
--Set account to LocalSystem

now, take the properties or serviceInstaller1, there are three properties you must set
  • Start Type
  • Service name and
  • Display Name
Start type refers to how one must start this service.
Service name refers to the name to be shown in the Process List and,
Display name is the name that will be displayed inside the Services list....

Now set Start type to "Automatic",
ServiceName to "someName" and
DisplayName to "dispName"

-------------------------------------------------
Now take Service1.cs .
In this file we will be adding controls... Inside this we can only add controls form inside the Command tab. This will not show any design of the controls... More will be background controls


So we will just check a FileSystemWatcher control.
FileSystemWatcher is a control through which we can monitor an entire drive...

Add a fileSystemWatcher to it.
Now take the properties of fileSystemWatcher Set the path to the Folder or the Drive you need to Monitor...
Set EnableRaisingEvents to true...
IncludeSubDirectories to True.

There is an option also called notify Filter you can set it to anything you need.

Now take the Events of the FileSystemWatcher by clicking the Event tab in the Properties window...
Double click on the "created" event so that you get the fileSystemWatcher1_Created event.

now inside the event write these codes

-------------------------
System.IO.StreamWriter sw = new System.IO.StreamWriter(@"C:\sm.txt", true);
sw.WriteLine(e.ChangeType + " " + e.FullPath);
sw.Flush();
sw.Close();
-------------------------
This will create a file when there is change in the directory we have selected.


So now you have created a windows service... This cannot be executed directly. So we must install this service to our machine and start it.
So, First of all Build the solution... now we must install it..
For that Take the VisualStudio Command Prompt
Inside it navigate to the folder inside which we have created the service.
Now goto the debug folder ...
eg: the path will be like this

D:\Smilu\windowService\bin\debug>

now if you are inside the debug folder you can see the .exe files of your service that we need to install

For installing it type the command like this

installutil /i smilu.exe

  • /i - will install the service
  • /u - will unistall the service
-------------------------
D:\Smilu\windowService\bin\Debug>installutil /i smilu.exe
-------------------------
Here smilu.exe is the service name.

Now restart the machine... The service gets automatically started when we starts the windows.

or

Go to the control panel-->Administrative Tools-->services
Inside it there will be your service listed...
Right click on it and start the service...

Now create a file in the Directory you have mentioned for fileSystemWatcher..
and see the new file created with sm.txt in C:\.
This windows service will not be shown anywhere... It'll be working in the back ground as a process.....
To see this process Take TaskManager and see the Process tab inside it....
You can see the Process inside it.....


Now The most important thing... If you need to modify the WindowsService you first need to Uninstall the service and only after that you should create it or install it...

for unistalling it you can use

installutil /u smilu.exe

If you install a service already running it may cause upto a System crash on your machine...

The steps should be the exact cycle like this

1) Create a Windows Service
2) Install a Windows Service and use it
and now if u need to modify
1) Uninstall the running service..
2) Build the service
3) Install it again


-----------------------------------------------------------------------------------------------

No comments: