FileSystemWatcher的观看UNC路径 [英] FileSystemWatcher to watch UNC path
问题描述
有关于这个主题的问题不缺,但我仍然有问题。这里是我的情况。我有我需要注意的是在配置文件中指定的路径的服务。当我用一个本地驱动器,它的伟大工程。
然而,当我将其更改为类似\\\\服务器2 \\辅助\\ TEMP \\ watch_folder服务没有启动。在日志中的错误是目录名\\\\服务器2 \\辅助\\ TEMP \\ watch_folder是无效的。如果我复制直接进入Windows资源管理器的文件夹打开罚款。如果我把我的code并将其粘贴到一个旧的WinForms应用程序,它工作正常。我已经尝试了所有的的登录为帐户。我将它设置为使用管理员帐户,但仍然没有骰子。
下面是我的code:
_watcher =新FileSystemWatcher的();
_watcher.Path = ConfigurationManager.AppSettings [WatchFolder];
_watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName;
_watcher.Filter =* .TXT
_watcher.Created + =新FileSystemEventHandler(OnCreated);
_watcher.Error + =新ErrorEventHandler(的OnError);
_watcher.EnableRaisingEvents = TRUE;
任何想法?我不知所措,在这一点上我想我被它太长时间盯着。我衷心AP preciate任何帮助。
谢谢,
尼克
的 修改的
这里是个例外:
服务不能启动。 System.ArgumentException:目录名\\\\服务器2 \\辅助\\ TEMP \\ watch_folder是无效的。
在System.IO.FileSystemWatcher.set_Path(字符串值)
在FileWatcher.FileWatcher.Watch()
在FileWatcher.FileWatcher.OnStart(字串[] args)
在System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(对象状态)
我只是尝试这样的:
VAR _watcher =新FileSystemWatcher的();
_watcher.Path = @\\\\ 10.31.2.221 \\共享\\;
_watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName;
_watcher.Filter =* .TXT
_watcher.Created + =新FileSystemEventHandler((X,Y)=> Console.WriteLine(创建));
_watcher.Error + =新ErrorEventHandler((X,Y)=> Console.WriteLine(错误));
_watcher.EnableRaisingEvents = TRUE;
Console.ReadKey();
这是没有问题的作品,但是我复制你的异常就在:
- 运行用户没有权限来读取远程文件夹。
- 远程文件夹不存在。
您的问题肯定与权限有关,我认为正在运行的用户没有所需的权限。
另一件事,你可以尝试在远程文件夹映射到一个地方。
在cmd执行此:
NET USE Z:\\\\服务器2 \\辅助\\ TEMP \\ watch_folder /用户:域\\用户名密码
然后在你的code:
_watcher.Path = @Z:\\;
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_folder the service does not start. The error in the log is "The directory name \\server2\secondary\temp\watch_folder is invalid." 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.
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
EDIT Here 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)
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:
NET USE Z: \\server2\Secondary\temp\watch_folder /user:Domain\UserName Password
Then in your code:
_watcher.Path = @"Z:\";
这篇关于FileSystemWatcher的观看UNC路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!