Creating a File Watcher Service

To illustrate the outlined steps, the following example monitors a particular directory and reacts when a new or changed file is placed in the directory. The example Windows Service application waits for those files, extracts information from them, and then logs an event to a system log to record the file change.

Creating a Solution for the Windows Service

First, you need an appropriate solution in place to hold the Windows Service. To do so, follow these steps:

1. Create a new Windows Service project using Visual Studio. Name the project ProVB_FileWatcherService.
2. In the Solution Explorer, rename Service1.vb to FileWatcherService.vb. You will be prompted to also change the class name.
3. Click the design surface for FileWatcherService.vb. In the Properties window, change the ServiceName property from Service1 to FileWatcherService. Step 2 changes the name of the class on which the service is based, while the ServiceName property changes the name of the service as shown in the Service Control Manager.
4. Add an installer to the project. Go back to the design surface for FileWatcherService and right-click it. Select Add Installer. A new file called ProjectInstaller.vb is created and added to the project. The ProjectInstaller.vb file has two components added to its design surface: ServiceProcessInstaller1 and ServiceInstaller1.
5. On the ProjectInstaller.vb design surface, highlight the ServiceProcessInstaller1 control. In its Properties window, ...

Get Professional Visual Basic 2012 and .NET 4.5 Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.