FileSystemWatcher to watch UNC path

ぐ巨炮叔叔 提交于 2019-11-26 17:39:03

问题


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)


回答1:


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:\";



回答2:


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.




回答3:


I found a really cool way to get UNC with credentials working with FileSystemWatcher in a windows service on codeproject.

see Adrian Hayes post: http://www.codeproject.com/Articles/43091/Connect-to-a-UNC-Path-with-Credentials

His solution works a treat.




回答4:


I also ran into this problem. My fix was to include our company's domain name in the server path:

\\servername.company.com\directorytowatch



回答5:


You may need to have your path as the below:

\\\\server2\\Secondary\\temp\\watch_folder

or

@"\\server2\Secondary\temp\watch_folder"


来源:https://stackoverflow.com/questions/11219373/filesystemwatcher-to-watch-unc-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!