I used C # In vs2008 to write Windows service monitoring inventory and sent text messages based on the relevant logic. Now, in. net4.0 and vs2010, use C # To write the specified directory file for Windows service monitoring.
The procedure is as follows:
1. Establish a windowsserivce Project
Create a Windows Service Project in the Visual C # Windows Directory: "hellowindowsservice ". Add an app. config (configuration file, for example, specifying a monitoring directory and an errorlog.txt file (recording service operation logs ). As follows:
Note: To facilitate the calling of the app. config configuration file, system. configuration. dll must be introduced in the project.
2. Compile the core monitoring directory for windowsserivceCode
The Code is as follows:
The recorderrorlog function records the specific monitoring content of Windows service. Onfilecreated is the monitoring directory. If a new file is created, the event is triggered. The filesystemwatcher class is used here.
Windowsservice call code:
3. add installer for windowsserivce
In step 2, you have completed writing simple monitoring code for directory monitoring events. In this case, you also need to add the installer for the service. In the installer, you must set the service name, windowsservice description, and other information. The specific steps can be completed in the visual vs2010 interface. The procedure is as follows:
1. Open the service1 design interface and right-click
2. Select add installer
3. Set Related Properties
As follows:
4. Package Windows Service
Here, we use Visual Studio installer to package the hellowindowsservice. Create a Visual Studio installer project in the solution. Right-click the project and choose add ---> project output. As follows:
Click OK:
Then you need to add the installation and uninstallation functions for Windows service. Right-click the project file (hellowindowsservicesetup project), select view, and click custom actions:
Right-click "Install" and select "add M action". Click "OK" twice without any settings to perform the same operation on "Uninstall. As follows:
After completing the preceding steps, you must upload an existing file in the installation project. The file contains the log file "errorlog.txt" in hellowindowsservice ".
Now, the installation and uninstallation functions have been added for hellowindowsservice.
V. Summary
After completing the preceding steps, you also need to set the account attribute of serviceprocessinstaller1 of projectinstaller to LocalSystem. In this way, you do not need to enter the Windows account and password each time you install the service. In addition, if you need to automatically start the service after the installation is successful, you also need to modify projectinstaller. CS. Add the afterinstall event to the event and add the startup service code. In this way, the service can be automatically started after installation.
It is convenient and easy to compile Windows service under. net. If you are interested in writing Windows service under. net, try it.