• λ我爱Aspx >> Asp.Net >> Create and Control Windows Services--Add File-Monitoring Functionality[等级:高级]
  • Create and Control Windows Services--Add File-Monitoring Functionality[等级:高级]

  • :aspxer  Դ:internet  :2007-5-28 20:37:30  ؼ:
  • Create and Control Windows Services--Add File-Monitoring Functionality[等级:高级]

    that all the necessary architecture is in place, you can add some functionality to the service. As an example, I@#ll show you how to build a file-monitoring service that monitors the local file system and notes important 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 the system and to applications that want to start it programmatically. For this example, the ServiceName property is "File Monitor Service." Another property is CanPauseAndContinue—set this to true so your service can support pause and continue functionality.

    The OnStart() function performs the necessary initialization to start file monitoring as well as create the custom performance counters on the local system if they haven@#t been created already. The .NET Framework monitors local file system changes by implementing the FileSystemWatcher class, defined in the System.IO namespace, which allows you to specify handlers for file change notification events. In this case, you@#re interested in files that are created, changed, deleted, or renamed. Because you want to monitor file changes only on the local file system, you must enumerate all the defined drives and create a FileSystemWatcher object only on fixed drives. But the .NET Framework (as of this writing) doesn@#t contain any special functions to do this. So you must invoke the GetDriveType() API call defined in kernel32.dll on each drive in the array created from the call to GetLogicalDrives(). You can create a small class called PlatformInvokeKernel32 to wrap the API and call GetDriveType() easily:

    Ҷƪл˵?
  • һƪCreate and Control Windows Services--Control Your Service[等级:高级]
    һƪCreate and Control Windows Services--introduction[等级:高级]