FileSystemWatcher的多个实例?????? [英] Multiple instances of FileSystemWatcher ??????
问题描述
我一直在努力尝试编写目录观察器服务。
其中一个要求是它能够观看多个
目录,而不是子目录一个父目录,但只是
多个目录。
我遇到了障碍,不知道如何绕过它。
基本上我在app.config的目录列表中读到了
将它们放入一个数组中,所以我有这样的东西:
sDirsToWatch [0] =" C:\ Temp"
sDirsToWatch [1] =" D:\ Temp"
然后我执行以下操作
For(i = 0; i< sDirsToWatch.Length; i ++)
{
FileSystemWatcher myWatcher = new System.IO.FileSystemWatcher();
myWatcher.NotifyFilter = NotifyFilters.DirectoryName |
NotifyFilters.FileName | NotifyFilters.Attributes;
myWatcher.Deleted + = new FileSystemEventHandler(LogDeleted);
myWatcher.Path = sDirsToWatch [i];
myWatcher。 EnableRaisingEvents = true;
}
现在当它到达.Path行或者到达
时..EnableRaisingEvents为第二个目录,我收到一个错误:错误
读取D:\ Temp目录
我认为发生了什么是全部我正在创建的对象
被命名为相同,因此导致问题。
所以我的问题是,如何动态命名我的FileSystemWatcher
对象?
我们非常感谢任何帮助。
谢谢
大卫
大卫,
它看起来不像情况就是这样。你是在
服务中运行吗?您确定自己可以访问驱动器(如果它是一个
服务,那么默认情况下它可能不会)。另外,你确定
目录本身是否存在?
希望这会有所帮助。
-
- Nicholas Paldino [.NET / C#MVP]
- mv*@spam.guard.caspershouse。 com
" David" <哒************** @ gmail.com>在消息中写道
news:11 ********************* @ g14g2000cwa.googlegro ups.com ...我一直在努力尝试编写目录观察器服务。
其中一个要求是它能够观看多个目录,而不是一个父目录的子目录,但只是
我遇到了麻烦,不知道怎么解决它。
基本上我从应用程序的目录列表中读到。配置和填充它们成为一个数组,所以我有这样的东西:
sDirsToWatch [0] =" C:\ Temp"
sDirsToWatch [1] =" D: \ Temp"
然后我做以下
For(i = 0; i< sDirsToWatch.Length; i ++)
{/> FileSystemWatcher myWatcher = new System.IO.FileSystemWatcher();
myWatcher.NotifyFilter = NotifyFilters.DirectoryName |
NotifyFilters.FileName | NotifyFilters.Attributes;
myWatcher.Deleted + = new FileSystemEventHandler(LogDeleted);
myWatcher.Path = sDirsToWatch [i];
myWatcher.EnableRaisingEvents = true;
}
现在当它到达.Path行或者当它到达第二个目录的
.EnableRaisingEvents时,我收到一个错误:错误
读取D:\ Temp目录
我认为正在发生的事情是我创建的所有对象都被命名为相同,因此导致问题。
所以我的问题是,我如何动态命名我的FileSystemWatcher
对象?
任何帮助都将非常感激。
谢谢
大卫
Nicholas:
尚未将此作为服务运行,直到我将代码确定为止我将
我正在从Windows窗体应用程序执行此操作,但是一旦我获得代码工作,它将被移动到
服务。
两者都是我的本地硬盘驱动器上存在irectories,我可以自由复制并从他们那里删除文件。
感谢您的帮助
David
David,
我认为你不能看一个观察者多个目录。
尝试为你想要观看的每个目录创建一个新实例。
我认为问题在于你每次登顶时都会遇到问题。循环,你
让myWatcher指向一个新对象,而旧的对象被摧毁了
,因为没有任何东西再引用它。
Andy
I have been working on trying to write a directory watcher service.
One of the requirments is that it be able to watch multiple
directories, not sub directories of one parent directory, but just
multiple directories.
I have hit a snag and don''t know how to get around it.
Basically I read in a list of directories from the app.config and stuff
them into an array, so that I have something like this:
sDirsToWatch[0] = "C:\Temp"
sDirsToWatch[1] = "D:\Temp"
Then I do the following
For (i = 0; i < sDirsToWatch.Length; i++)
{
FileSystemWatcher myWatcher = new System.IO.FileSystemWatcher();
myWatcher.NotifyFilter = NotifyFilters.DirectoryName |
NotifyFilters.FileName | NotifyFilters.Attributes;
myWatcher.Deleted += new FileSystemEventHandler(LogDeleted);
myWatcher.Path = sDirsToWatch[i];
myWatcher.EnableRaisingEvents = true;
}
Now when it gets to either the .Path line or to when it gets to the
..EnableRaisingEvents for the 2nd directory, I get an error: Error
reading the D:\Temp directory
I think what is happening is that all of the objects that I am creating
are named the same, so its causing problems.
So my question is, how do I dynamically name my FileSystemWatcher
object ?
Any help would be most appreciated.
Thanks
David
David,
It doesn''t look like this is the case. Are you running this in a
service? Are you sure that you have access to the drive itself (if it is a
service, then it might not, by default). Also, are you sure that the
directory itself exists?
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"David" <da**************@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...I have been working on trying to write a directory watcher service.
One of the requirments is that it be able to watch multiple
directories, not sub directories of one parent directory, but just
multiple directories.
I have hit a snag and don''t know how to get around it.
Basically I read in a list of directories from the app.config and stuff
them into an array, so that I have something like this:
sDirsToWatch[0] = "C:\Temp"
sDirsToWatch[1] = "D:\Temp"
Then I do the following
For (i = 0; i < sDirsToWatch.Length; i++)
{
FileSystemWatcher myWatcher = new System.IO.FileSystemWatcher();
myWatcher.NotifyFilter = NotifyFilters.DirectoryName |
NotifyFilters.FileName | NotifyFilters.Attributes;
myWatcher.Deleted += new FileSystemEventHandler(LogDeleted);
myWatcher.Path = sDirsToWatch[i];
myWatcher.EnableRaisingEvents = true;
}
Now when it gets to either the .Path line or to when it gets to the
.EnableRaisingEvents for the 2nd directory, I get an error: Error
reading the D:\Temp directory
I think what is happening is that all of the objects that I am creating
are named the same, so its causing problems.
So my question is, how do I dynamically name my FileSystemWatcher
object ?
Any help would be most appreciated.
Thanks
David
Nicholas:
Not running this as a service yet, until I get the code nailed down I
am doing it from a Windows Form application, but it will be moved to a
service once I get the code working.
Both directories exist on my local hard drive and I can freely copy and
delete files from them.
Thanks for the help
David
David,
I don''t think you can have one watcher watching multiple directories.
Try creating a new instance for each directory you would like to watch.
I think the problem is that everytime you hit the top of your loop, you
make myWatcher point to a new object, and the old one gets destoryed
since nothing is referencing it any longer.
Andy
这篇关于FileSystemWatcher的多个实例??????的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!