Microsoft introduced long running background application called Services along with Windows NT Operating System. These services are multithreaded. Writing a windows NT Service using VB 6 was a tedious job. However, VB.NET masks the task much easy. In this article I will show you how to create a simple Windows Service using VB.NET
Overview of Services
Windows service is a background process that performs certain task. You can start, stop, pause or resume services on remote and local computers. Windows also provide option to enable or disable a service. Objective of Windows service is to run application in the background in certain period even though machine is busy with other process or work. You can also set a user account that a service will use to log on.
Is Windows NT Service having an advantage over application running under Windows Scheduler?
As mentioned earlier, windows services are used to do certain task on regular intervals. Is this can be achieved by other way? Actually Yes. You can create an application for performing your task. Using windows scheduler you can call this application at regular intervals. What is the advantage of implementing Windows Service then? Normally windows services will not have a user interface. Windows services runs efficiently because of its own process. To start windows Scheduler, one of the user has to log on to system. Whereas Windows service can automatically started when system boots.
Creating a simple windows service
Before we start developing windows service one thing you have to remember. You can deploy the service only on OS that has NT based architecture. If you are working in Visual Studio .Net, you can see a windows service template on new project dialog. You have to code very less to test this simple windows service if you are using VS.NET. In this article we are going to create a simple windows service which will monitor a specific folder on the disk. If any file is renamed, created, modified or deleted, service will keep its track in the system log.
Open your VS.NET and select windows service project and enter the name folderSPY and click ok. Double click the design form or Press Shift + F7. On the top section of code window, enter the following:
Public folderToWatch as fileSystemWatcher
On the OnStart procedure type the following
Dim strSysDir As String
folderToWatch = new fileSystemWatcher()
To watch system directory
strSysDir = System.Enviroment.SystemDirectory()
Set the path of the folder which you want to watch. Here our service is watching WINNT folder.
folderToWatch.path = strSysDir
To monitor above mentioned folder, you have to set certain filters.
.notifyFilter = .notifyFilter
.notifyFilter = .notifyFilter
'To watch sub directories....
.includeSubDirectories = True
How to know whether any user changed the file name or file attributes?
To know this you need handlers. Following code will explain this.
AddHandler folderToWatch.Created, Addressof newLog
AddHandler folderToWatch.Deleted, Addressof newLog
folderToWatch.enableRaisingEvent = True
Next, write a sub procedure called newLog.
Private Sub newLog(byVal Source as Object,
byVal evt as FileSystemEventArgs)
If evt.ChangeType = WatcherChangeTypes.Created Then
else if evt.ChangeType = WatcherChangeTypes.Deleted
Private Sub writeToLog(byVal filePath as String,
byVal fileManip as String)
Dim evtLog as new EventLog()
If not evtLog.sourceExists("folderSPY") then
("folderSPY", "Log of folderSPY")
evtLog.source = "folderSPY"
("Folder spy Log", "File " & filePath & " has "
& fileManip & " by " & Environment.UserName.ToString,
Installing the folderSPY windows service
The application that we have created is ready to test. Since it is not a traditional windows executable, you need to install it on your machine by special way. .NET Framework ships with InstallUtil.exe command line tool that installs the windows service on your machine. Before using this tool, however, we have to add an installer toy your project. This can done by right clicking your design screen and select add installer option from the menu. After you do that, you can see two more controls called ServiceProcessInstaller1 and ServiceInstaller1 added to your screen. To Change the display name of the service, open the ServiceInstaller1 control's property window and change the ServiceName property and Display name property to folderSPY (This is the service name we have given).
Since we are going to run our application on the local machine, set the account property of the ServiceProcessInstaller1 to LocalSystem. To make your service pause and continue, set the CanPauseandContinue property of your service to true. Now, compile the folderSPY application and run following command from Command Prompt:
InstallUtil <Project Path>\BIN\folderSPY.exe
If you want to uninstall the service use:
InstallUtil /U \BIN\folderSPY.exe
Running the Service
If Startup property of the service is not set to Automatic, you have to start the service manually or through some other program. To start the service open Services snap-in from the Control panel. Right click on the folderSPY service and click start.