Create and Control Windows services--add file-monitoring functionality[level: advanced]

Source: Internet
Author: User
Tags file system implement integer reset
Services|window| advanced now and all the necessary architecture are in place, the can add some functionality to the service. As an example, I'll show you and the A file-monitoring service which monitors the local file system and notes Importan T activity by presenting the "data" with a custom performance counter.

The ServiceBase class provides a couple properties all services should implement. ServiceName is the ' name your service uses to identify itself to ' system and to ' applications that ' want to start it progr Ammatically. For this example, the ServiceName property is "File Monitor Service." Another property is Canpauseandcontinue-set this to true your service can support pause and continue.

The OnStart () function performs the necessary initialization to start file monitoring as OK as create the custom perform ance counters on the local system if they haven ' t been created. The. NET Framework monitors the local file system changes by implementing the FileSystemWatcher class, defined in the SYSTEM.I O namespace, which allows to specify handlers the for file change notification events. In this case, you are ' re interested in the files that are created, changed, deleted, or renamed. Because you want to monitor file changes in the local file system, you must enumerate all defined drives and CREA Te a FileSystemWatcher object is on fixed drives. But the. NET Framework (as of this writing) doesn ' t contain no special functions to doing this. So your must invoke the GetDriveType () API call defined in Kernel32.dll on each drive in the array created GetLogicalDrives (). Can create a small class called PlatformInvokeKernel32 to wrap API and call GetDriveType () Easily:

public class PlatformInvokeKernel32
{
[Sysimport (dll= "kernel32", CharSet=System.Runtime.InteropServices.CharSet.Auto)]

public static extern int
GetDriveType (string lprootpathname);
}
This code simply forwards all calls using Platforminvokekernel32.getdrivetype () to the associated function in Kernel32.dll . To get the drive type for each drive in the array, check the value returned from the call to the enumerated type win from The Microsoft.Win32.Interop namespace:

string[] drives = Environment.getlogicaldrives ();

Create filesystem watchers for
Local fixed drives
foreach (string curdrive in drives)
{
if (
Platforminvokekernel32.getdrivetype
(curdrive) = win. drive_fixed)
{
Create a FileSystemWatcher
}
}
Now so you ' ve created the FileSystemWatcher objects, and you must implement the event handlers for the four file Notificatio n events:creating, changing, deleting, and renaming files. You'll create performance counters that convey information to the user Visually-one for each event. For more information in file notification event handlers and creating performance counters, download the code that ACCOMPA NIEs this article. To create a performance counter with the PerformanceCounter class specified in the System.Diagnostics namespace. When you call the "File Change event handler", it simply increments the proper performance counter by 1:

private void onfilechanged (Object source, FileSystemEventArgs E)
{
Filechangecounter.incrementby (1);
}
The ServiceBase class also enables the service designer to create custom commands this service control Manager can Sen D to the service. This is a overridden but optional function named Oncustomcommand (), which I mentioned earlier. It contains a single parameter, an integer, which signifies the custom command to run. One thing you need to keep in mind:the command integer must is a value between 128 and 256; All other values are reserved by the system. For the file-monitoring service you ' re building, your create four custom commands to reset the counter data for each of the Performance counters back to zero. The PerformanceCounter class doesn ' t support a ' Reset ' function specifically, but you can accomplish this easily by Decrem Enting the counter by its value (Listing 1).



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.