VSM - March 2003 Getting Started column
Create a Windows Service App
Stan Schultes - stan@vbnetexpert.com

This sample requires Windows NT, Windows 2000 or Windows XP to run a Windows Service app. The sample code is built using Visual Studio .NET. You should apply the lastest SP to the Framework (SP2 as of this writing). 

The sample code for this column includes two applications - the FileChangeMonitor Service app, and the FileChgCtl utility. FileChangeMonitor is the focus of this article, and FileChgCtl is a special Service Control app that sends custom commands to FileChangeMonitor. Unzip the projects into their two directories to get started.

When you want to register your Windows Service app, you use the InstallUtils command-line utility. Start a Command Prompt window and navigate to the \bin directory. Visual Studio comes with a file named corvars.bat that you can use to set environment and path variables needed for the command-line utilities. You find corvars.bat in the \Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Bin directory. I put a copy of corvars.bat in my Windows directory so I can execute it from anywhere. 

You register your Service app like this:
>InstallUtil FileChangeMonitor.exe

Use the Service Control Manager (Control Panel -> Administrative Tools) to start, stop, pause and resume your Service app. When time comes to uninstall the Service (to replace it with a new version, for example), first stop the Service with SCM and exit SCM. Unregister the Service like this: 
>InstallUtil FileChangeMonitor.exe /u

You can then build and re-register your Service app.

The sample code runs from a settings file named FileChangeMonitor.xml, found in the project's \bin directory. Get started by starting and stopping the registered Service, this creates a default settings file (you can change these default settings in the InitSettings function). Edit this file to define your Service settings. Be sure to stop the Service app when you edit the settings file, otherwise your changes will get overwritten when state is saved. 

The default filename to monitor is FileChangeMonitor.txt, also in the project's \bin directory. While testing, change this file within the file check interval to keep the alarm action from occurring. You can create as many MonitorFile sections as you need for unique files to monitor.

In the settings, pay particular attention to the email settings. Set the SMTPServer setting to the address of an SMTP on which you have permission to send messages. Use 127.0.0.1 for the local machine if you have your own SMTP server running (as part of Microsoft Internet Information Services). Set the EmailToList to a semicolon-delimited list of email addresses you want to notify on alarm or summary actions. Finally, set the EmailFrom setting to an email address in your domain.

You can use the FileChgCtl app to send custom command messages to FileChangeMonitor. Command 2, for example, causes FileChangeMonitor to send a test email message so you can test your email setup. Check the source code for the other debug functions in FileChgCtl.

