在 C# 中的单个文件上使用 FileSystemWatcher

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12798197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:30:18  来源:igfitidea点击:

Use FileSystemWatcher on a single file in C#

c#.netfilesystemwatcher

提问by Jimmy

When i try to set the watcher path to a single file like so:

当我尝试将观察者路径设置为单个文件时,如下所示:

watcher.Path = filePath1;

I get the error:

我收到错误:

The directory name C:\Cromos 3.0\repository\diagnostics\dwm01_2011_06_13__09_03.LXD is invalid.

Can you only set the path to a folder directory?

你只能设置文件夹目录的路径吗?

采纳答案by Steve

Your error is setting the Path property with a full filename

您的错误是使用完整文件名设置 Path 属性

watcher.Path = Path.GetDirectoryName(filePath1); 
watcher.Filter = Path.GetFileName(filePath1);

should work.

应该管用。

Not related to your proper question, but, of course, as stated in below comments, it is imperative to set the EnableRaisingEventsproperty to true to enable the FileSystemWatcher's functionality

与您的正确问题无关,但是,当然,如下面的评论所述,必须将EnableRaisingEvents属性设置为 true 以启用 FileSystemWatcher 的功能

回答by Justin Harvey

Yes, but you can watch for specific files by setting the filter property to the filename.

是的,但是您可以通过将过滤器属性设置为文件名来监视特定文件。

e.g.

例如

watcher.Filter = "dwm01_2011_06_13__09_03.LXD";