This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Programming
.NET 1.1+

Creating a Windows Service

Windows services are background processes that usually have no direct interaction with the user interface. This article explains how to create a windows service with an example that monitors the file system for changes using a FileSystemWatcher object.

Installing the Service

With the installer added, we can now install the service. First, compile the code. This will generate the executable file that will be installed.

The service will access the watched folder and the executable files of the service itself using the user account specified earlier. To ensure that this is possible, check the permissions of this folder and the service's files and ensure that they can be read by the local service account.

Services are installed using a command-line program named "InstallUtil". This utility is shipped with the .NET framework. The easiest way to obtain access to it is using the Visual Studio command prompt tool. This special version of the command prompt utility has predefined paths configured to simplify the use of tools such as InstallUtil.

Open a new Visual Studio command prompt by selecting the following Start Menu options. Start, All Programs, Visual Studio, Visual Studio Tools, Visual Studio Command Prompt. NB: If you have the option, right-click the icon and run the program as an administrator. If you do not have this option, you must be an administrator to successfully complete the installation.

Within the command prompt window, change the current folder to that of the compiled executable for the service. You can now install the service by executing the following command:

installutil FileMonitorService.exe

If the process is successful, open the Services utility from the Administrative Tools section of the Control Panel. The list of services should include a new item named "File Monitor Demonstration Service". This is the descriptive name we specified earlier.

Starting the Service

You can now test the service. Using the Services window, try starting the service. You should find that a new event is logged and can be viewed using the Event Viewer. Try creating, modifying, renaming and deleting files to see further events logged. Try pausing the service to suspend logging events temporarily, then resume to continue logging.

Uninstalling the Service

Once you have finished with the service, you can uninstall it. First, ensure the service is stopped. You can then remove it by executing the following command at the Visual Studio command prompt:

installutil -u FileMonitorService.exe
10 September 2008