C# FileSystemWatcher 监视 UNC 路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11219373/
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
FileSystemWatcher to watch UNC path
提问by nickfinity
There are no shortage of questions on this topic, but I'm still having trouble. Here is my situation. I've got a service that I need to watch a path that is specified in the config file. It works great when I used a local drive.
关于这个主题的问题不乏问题,但我仍然遇到麻烦。这是我的情况。我有一个服务,我需要监视配置文件中指定的路径。当我使用本地驱动器时,它工作得很好。
However, when I change it to something like \\server2\secondary\temp\watch_folderthe service does not start. The error in the log is
但是,当我将其更改\\server2\secondary\temp\watch_folder为服务未启动之类的内容时。日志中的错误是
The directory name \\server2\secondary\temp\watch_folder is invalid.
目录名称 \\server2\secondary\temp\watch_folder 无效。
If I copy that directly into Windows Explorer the folder opens fine. If I take my code and paste it into an old Winforms app it works fine. I've tried all of the "Log On As" accounts. I set it to use the Administrator account, but still no dice.
如果我将其直接复制到 Windows 资源管理器中,则该文件夹可以正常打开。如果我将我的代码粘贴到旧的 Winforms 应用程序中,它就可以正常工作。我已经尝试了所有的“登录身份”帐户。我将它设置为使用管理员帐户,但仍然没有骰子。
Here is my code:
这是我的代码:
_watcher = new FileSystemWatcher();
_watcher.Path = ConfigurationManager.AppSettings["WatchFolder"];
_watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName;
_watcher.Filter = "*.txt";
_watcher.Created += new FileSystemEventHandler(OnCreated);
_watcher.Error += new ErrorEventHandler(OnError);
_watcher.EnableRaisingEvents = true;
Any ideas? I'm at a loss and at this point I think I've been staring at it too long. I sincerely appreciate any help.
有任何想法吗?我不知所措,在这一点上,我想我已经盯着它看得太久了。我真诚地感谢任何帮助。
Thanks, Nick
谢谢,尼克
EDITHere is the exception:
编辑这里是例外:
Service cannot be started. System.ArgumentException: The directory name \server2\Secondary\temp\watch_folder is invalid.
at System.IO.FileSystemWatcher.set_Path(String value)
at FileWatcher.FileWatcher.Watch()
at FileWatcher.FileWatcher.OnStart(String[] args)
at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)
服务无法启动。System.ArgumentException: 目录名 \server2\Secondary\temp\watch_folder 无效。
在 System.IO.FileSystemWatcher.set_Path(String value)
在 FileWatcher.FileWatcher.Watch()
在 FileWatcher.FileWatcher.OnStart(String[] args)
在 System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)
采纳答案by Marco Medrano
I just tried this:
我刚试过这个:
var _watcher = new FileSystemWatcher();
_watcher.Path = @"\10.31.2.221\shared\";
_watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName;
_watcher.Filter = "*.txt";
_watcher.Created += new FileSystemEventHandler((x, y) =>Console.WriteLine("Created"));
_watcher.Error += new ErrorEventHandler( (x, y) =>Console.WriteLine("Error"));
_watcher.EnableRaisingEvents = true;
Console.ReadKey();
That works without problems, however i replicated your exception just when:
这没有问题,但是我在以下情况下复制了您的异常:
- The running user doesn't have permissions to read the remote folder.
- The remote folder doesn't exist.
- 运行用户无权读取远程文件夹。
- 远程文件夹不存在。
Your problem surely is related with permissions, I think that the running user doesn't have the permissions needed.
您的问题肯定与权限有关,我认为正在运行的用户没有所需的权限。
Another thing that you can try is map the remote folder to one local.
您可以尝试的另一件事是将远程文件夹映射到一个本地文件夹。
Execute this in the cmd:
在cmd中执行:
NET USE Z: \server2\Secondary\temp\watch_folder /user:Domain\UserName Password
Then in your code:
然后在你的代码中:
_watcher.Path = @"Z:\";
回答by John Koerner
Your service is probably running under a user account that does not have permission to that share. Try changing the windows service to run under different credentials.
您的服务可能在没有该共享权限的用户帐户下运行。尝试更改 Windows 服务以在不同凭据下运行。
回答by Thor Burfine
You may need to have your path as the below:
您可能需要拥有如下路径:
\\server2\Secondary\temp\watch_folder
or
或者
@"\server2\Secondary\temp\watch_folder"
回答by John
I found a really cool way to get UNC with credentials working with FileSystemWatcher in a windows service on codeproject.
我找到了一种非常酷的方法,可以在 codeproject 的 Windows 服务中使用 FileSystemWatcher 的凭据获取 UNC。
see Adrian Hayes post: http://www.codeproject.com/Articles/43091/Connect-to-a-UNC-Path-with-Credentials
见 Adrian Hayes 帖子:http: //www.codeproject.com/Articles/43091/Connect-to-a-UNC-Path-with-Credentials
His solution works a treat.
他的解决方案很有用。
回答by Chris
I also ran into this problem. My fix was to include our company's domain name in the server path:
我也遇到了这个问题。我的解决方法是在服务器路径中包含我们公司的域名:
\servername.company.com\directorytowatch

