26

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)

3per
  • 351
  • 9
  • 26
nickfinity
  • 1,119
  • 2
  • 15
  • 29
  • Do you mean the problem has been solved? – Marco Medrano Jun 27 '12 at 04:39
  • No, the problem has not been solved. I'm still unable to start the service. – nickfinity Jun 27 '12 at 04:42
  • 1
    Do you get any exception errors? Do you know how to debug a Windows Service? Any info on the errors helps. – The Original Android Jun 27 '12 at 05:50
  • 1
    I suspect there is something unique about directory \\server2\...watch_folder since other directories work, as you said. Review the read/write permissions for example. – The Original Android Jun 27 '12 at 05:52
  • 1
    Is the server you are wanting to watch running Windows? – Michael Jun 27 '12 at 06:27
  • server2 is running Windows Server 2003. Ok, I pointed the service to an old Vista machine and it worked. I'm not sure what the issue is with the other server, but at least I know my code should be good. I'll figure out the permissions issue now. I really appreciate all of your help. – nickfinity Jun 27 '12 at 13:33
  • possible duplicate of [FileSystemWatcher Fails to access network drive](http://stackoverflow.com/questions/960318/filesystemwatcher-fails-to-access-network-drive) – ChrisF Oct 01 '12 at 20:34

5 Answers5

24

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:\";
Marco Medrano
  • 2,530
  • 1
  • 21
  • 35
  • 1
    I had this issue and it was a permission issue on the folder. Thanks for the help! – liebs19 Sep 25 '14 at 17:45
  • I had this problem too. My service with a FileSystemWatcher was set up to monitor a folder on a network drive as NetworkService, **and the path has spaces in it so I thought that was the problem.** I was running it on my laptop. After reading your answer about the permissions, I installed the service with the FileSystemWatcher on the app server, and the problem disappeared! The service has the right permissions under NetworkService when running from that app server. - Helpful answer! – Greg Barth Jul 01 '15 at 21:33
  • @marco medrano c:\windows\system32\foldername this is on local system and it returns directory name doesn't exist , its working from visual studio but gives exception from a windows service.any idea? – Dragon May 17 '20 at 21:08
  • @Dragon I think you might be using a relative path rather than the absolute path (if it does work in VS) – Marco Medrano May 19 '20 at 23:40
6

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.

John Koerner
  • 37,428
  • 8
  • 84
  • 134
2

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.

John
  • 1,714
  • 21
  • 41
1

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

\\servername.company.com\directorytowatch
Chris
  • 1,533
  • 2
  • 17
  • 33
-1

You may need to have your path as the below:

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

or

@"\\server2\Secondary\temp\watch_folder"
James Bellaby
  • 158
  • 14
Thor Burfine
  • 124
  • 1
  • 7
  • You need more than two slashes in front of server2 to do it that way: Try \\\\server2\\Secondary\\temp\\watch_folder – Greg Barth Jul 01 '15 at 21:42
  • @JamesBellaby Rather than correct a wrong answer, I suggest writing your own instead. – Konrad Rudolph Oct 23 '19 at 10:12
  • 1
    @Konrad Rudolph Reasons to why it is wrong? You should elaborate more on why you think things are wrong in order to provide better direction for people reading the comments of a perceived incorrect answer... – James Bellaby Nov 18 '19 at 11:30
  • @JamesBellaby **You** suggested a correction to this answer, I assume you know what was wrong with the answer. – Konrad Rudolph Nov 18 '19 at 11:32
  • @Konrad Rudolph I corrected a typo. You're assuming it's incorrect. – James Bellaby Nov 18 '19 at 11:33
  • @Konrad Rudolph I'll not help in future to correct wrong answers and continue to bloat the answer sections. Or not do it in future at all to avoid the highly criticising community of StackOverflow. FYI does the answer look incorrect after I edited it? If not what is the problem? – James Bellaby Nov 18 '19 at 11:36
  • @JamesBellaby Well my opinion was clearly overruled by the other reviewers, who felt that your edit was valuable. For the record, I merely felt that it would have been more valuable as its own answer (because that associates the answer with *you* and gives you reputation when people upvote it), and that this answer, as it previously was, wasn’t helpful and could have been deleted instead. – Konrad Rudolph Nov 18 '19 at 11:37
  • @Konrad Rudolph OK but according to the dates it obviously wasn't going to get deleted any time soon. I won't help out next time. FYI i couldn't care less about the points system. It obviously clouds peoples judgments. – James Bellaby Nov 18 '19 at 11:41
  • @Konrad Rudolph I personally hate this reputation system of Stack overflow. For this very reason. I don't wont points I want correct information. – James Bellaby Nov 18 '19 at 11:44
  • @Konrad Rudolph You also need to rethink how you write comments. You came across as annoyed that I didn't do what you want. – James Bellaby Nov 18 '19 at 11:46
  • @JamesBellaby Comments are meant for *concise* communication. If you read annoyance into my comment that’s on you. By contrast, I can tell you, outright, that I am by now annoyed. – Konrad Rudolph Nov 18 '19 at 11:50
  • @Konrad Rudolph "Rather than correct a wrong answer, I suggest writing your own instead" vs "I think you would benefit from making this your own answer" That is about the same (concise), more polite and starts off being an opinion by mentioning that you think... etc. – James Bellaby Nov 18 '19 at 11:54