使用FileSystemWatcher监控多个硬盘 - C# [英] Monitor multiple hard drive using FileSystemWatcher - C#

查看:89
本文介绍了使用FileSystemWatcher监控多个硬盘 - C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi..all



我想使用C#创建一个应用程序(Visual Studio 2008),我的应用程序进程应该监视复制粘贴文件进程这是由当前登录的用户做的。



但问题是,我只提供一个到FileSystemWatcher对象的路径。我监视我的所有驱动器。但它无法处理我。在我的机器上有多个驱动器Ex:c:\,D:\等等......

那我怎么能给FileSystemWatcher.Path ????



我在服务start.Code上覆盖onstart mathod



Hi..all

I want to create an Application using C# (Visual Studio 2008),My app process is expected to monitor the copy-paste file process that is doing by the current logged user.

But the problem is that,I give only one path to FileSystemWatcher object. I monitor my all drive. But it can't posible to handle to me.In my machine have multiple drive Ex : c:\, D:\, etc...
So how can i give to FileSystemWatcher.Path ???

I override onstart mathod on my services start.Code is under

protected override void OnStart(string[] args)
{
	FileSystemWatcher.path = "C:\\"
}





任何解决方案都会得到我的赞赏。

谢谢..



Any solution would get my great appreciation.
Thanks..

推荐答案

你可以只需为每个驱动器使用FileSystemWatcher。只需创建一个列表或向量,并为每个驱动器添加一个FileSystemWatcher。



祝你好运!
You can simply use a FileSystemWatcher for each drive. Simply create a list or vector and for each drive you add a FileSystemWatcher.

Good luck!


来自其他人的回答,它对其他编码器很有用。



Answer from other guys, It helpful to other coder.

protected override void OnStart(string[] args)
{

string[] drives = Environment.GetLogicalDrives();

_watchers = new FileSystemWatcher[drivers.Length];

int i = 0;

foreach (string strDrive in drives)

{

FileSystemWatcher _watcher = new FileSystemWatcher();

_watcher.Path = strDrive;

_watcher.Changed += new FileSystemEventHandler(FolderWatcherTest_Changed);

_watcher.Created += new FileSystemEventHandler(FolderWatcherTest_Created);

_watcher.Deleted += new FileSystemEventHandler(FolderWatcherTest_Deleted);

_watcher.Renamed += new RenamedEventHandler(FolderWatcherTest_Renamed);

_watchres[i] = watcher;

//Begin watching.
watcher.EnableRaisingEvents = true;

i++;

}

}


这篇关于使用FileSystemWatcher监控多个硬盘 - C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆