国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

create and Control Windows Services--Add File-Moni

2024-07-21 02:21:29
字體:
來源:轉載
供稿:網友
now 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:

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 that you've created the filesystemwatcher objects, you must implement the event handlers for the four file notification 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 on file notification event handlers and creating performance counters, download the code that accompanies this article. to create a performance counter, use 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 that the service control manager can send to the service. this is an overridden but optional function named oncustomcommand(), which i mentioned earlier. it contains a single parameter, an integer, that signifies the custom command to run. one thing you need to keep in mind: the command integer must be a value between 128 and 256; all other values are reserved by the system. for the file-monitoring service you're building, you 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 decrementing the counter by its current value (see listing 1).


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 漯河市| 汝南县| 调兵山市| 花垣县| 舟曲县| 苏尼特左旗| 资溪县| 仁寿县| 滕州市| 英吉沙县| 驻马店市| 阆中市| 景泰县| 游戏| 甘南县| 新干县| 福安市| 霍林郭勒市| 连州市| 牟定县| 新泰市| 太湖县| 保定市| 望都县| 长兴县| 仁布县| 盖州市| 榆树市| 栾城县| 车险| 恩平市| 芒康县| 汝州市| 会昌县| 同江市| 耒阳市| 玛纳斯县| 西峡县| 道孚县| 岳普湖县| 濉溪县|