BlackWaspTM
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.

Windows Services

Windows services were introduced in Windows NT under the name, "NT Services". A service is a long-running executable program that executes in the background in its own Windows session. Services are ideal for monitoring applications and for reacting to events. In this article we will create a service that reacts to the creation, modification and deletion of files using the FileSystemWatcher class. Services can also be used for scheduled processes using timer events, though consideration should be given to using the Windows scheduling tools in these cases.

Services do not usually include any graphical user interface elements. Although it is possible to display dialog boxes from a correctly configured service, this is usually inappropriate. One of the primary considerations is that services do not require an interactive user to be logged on to Windows. In fact, when services are configured to start automatically, your service may be executing before the log on screen is displayed. For these reasons, direct user input is usually not included in a service. Output is generally logged to the event log, a file or a database.

Service Accounts

As services do not require an interactive user, they log on to Windows separately. Services may use a different account to that of any interactive user of the computer, potentially with different privileges. The user credentials for the service are supplied when it is installed. The account may be one of the following:

  • Standard User. Any user name and password may be provided to assign that user's permissions to the service. This option is usually used during development rather than for production systems. One problem is that if the user changes their password, the service must be reconfigured.
  • Local Service. The built-in local service account is specific to the local computer and has limited privileges. This account presents anonymous credentials to remote computers, restricting access to network resources. It is ideal for services that use only local resources.
  • Network Service. The network service account provides limited privileges to the local machine and remote computers. This account is ideal for services that require substantial access to network resources.
  • Local System. This built-in account provides full access to the local computer's resources, including control of the directory service if installed on a domain controller. It should be used with care as, if used maliciously, it has the capability of accessing the entire domain.

Microsoft Management Console

Windows services are installed differently than normal applications. A standard executable program may have a shortcut in the Start menu or on the desktop. A service is controlled using the Services snap-in for the Microsoft Management Console (MMC). This is accessed using the Services option from the Administrative Tools section in the Control Panel.

In the Services window, the full list of installed services is displayed. These can be started, stopped, paused and resumed using the snap-in tools. By modifying a service's properties, it can be made to start automatically when the operating system is booted, only start manually, or be disabled altogether. The service's user account and password may also be modified.

A full description of the Services snap-in is beyond the scope of this article. However, the basics of starting and stopping a service will be described.

Creating a Service

In this section we will create a Windows service using C# and Microsoft Visual Studio. The edition of Visual Studio is important, as we will be using the Windows Service template project. In some editions, such as Visual C# Express, this template is unavailable. You can create a service in Visual C# Express or another, non-Microsoft development environment but the process is more complicated.

The service that we will create will use a FileSystemWatcher object to monitor the contents of a folder. Whenever a change is detected in the folder, due to the creation, deletion, modification or renaming of a file, the update will be logged in the event log. In a real-world application, the service would perform some activity using the files. In this case, adding such functionality would only complicate the examples.

10 September 2008