6

I am trying to run a file watcher over some server path using windows service. I am using my windows login credential to run the service, and am able to access this "someServerPath" from my login. But when I do that from the FileSystemWatcher it throws:

The directory name \someServerPath is invalid" exception.

var fileWatcher = new FileSystemWatcher(GetServerPath()) 
    {
        NotifyFilter=(NotifyFilters.LastWrite|NotifyFilters.FileName),
        EnableRaisingEvents=true,
        IncludeSubdirectories=true
    };

public static string GetServerPath() 
{
    return string.Format(@"\\{0}", FileServer1);              
}

Can anyone please help me with this?

Rob
  • 45,296
  • 24
  • 122
  • 150
Manish Basantani
  • 16,931
  • 22
  • 71
  • 103

6 Answers6

9

I have projects using the FileSystemWatcher object monitoring UNC paths without any issues.

My guess from looking at your code example may be that you are pointing the watcher at the root share of the server (//servername/) which may not be a valid file system share? I know it returns things like printers, scheduled tasks, etc. in windows explorer.

Try pointing the watcher to a share beneath the root - something like //servername/c$/ would be a good test example if you have remote administrative rights on the server.

Lance McNearney
  • 9,410
  • 4
  • 49
  • 55
  • Thanks. This appears to be the problem here. – Manish Basantani Jun 07 '09 at 15:58
  • 1
    Doesn't seem to work at all with //tsclient/ shares over remote desktop. There are no errors, but when files change, the monitor doesn't pick anything up. If you try to map the //tsclient/ share to a drive, it maps fine, but FileSystemWatcher throws an error if you specify the mapped drive letter. – Triynko Apr 06 '12 at 06:07
5

With regards to the updated question, I agree that you probably need to specify a valid share, rather than just the remote server name.

[Update] Fixed previous question about the exception with this:

specify the name as @"\\someServerPath"

The \ is being escaped as a single \

When you prefix the string with an @ symbol, it doesn't process the escape sequences.

John Weldon
  • 39,849
  • 11
  • 94
  • 127
  • This one sounds right, but I would assume the path is actually a variable that is read in from a config file or database. Maybe he should show us the actual code ... – flipdoubt Jun 06 '09 at 19:36
  • Ofcourse i am doing the same. Also, i am using @"\\path" before the variable which returns this path. – Manish Basantani Jun 06 '09 at 20:18
3

I was just asked this question in regards to FileSystemWatcher code running as a service and the issue is permissions. I searched and found this question and answer but unfortunately none of the answers here solved the problem. Anyway, I just solved it, so I thought I would throw in the solution here for next guy who searches and find this question.

The drive was mapped as a logged in user but the service was running as LocalSystem. LocalSystem is a different account and does not have access to drives mapped by a user.

The fix is to either:

  1. Authenticate first (I use a C# Class to establish a network connection with credentials)
  2. Run your service as a user that has access to the share.

You can test LocalSystem authentication by using a LocalSystem command prompt, see How to open a command prompt running as Local System?

Rhyous
  • 6,510
  • 2
  • 44
  • 50
  • The best solution could be using the **NetworkService** account with the right permissions on the drive/folder. A link on the subject : [Using NetworkService](http://stackoverflow.com/questions/11978054/cannot-start-windows-service-in-networkservice-account) – Ethenyl Mar 28 '13 at 10:13
  • 1
    I think this one is the correct answer, I'm getting the same problem, If i execute the service as console app is working fine, otherwise it's not able to find the network path. – Mauro De Biasio Jul 21 '14 at 07:49
2

Even though this is already answered I thought I would put in my two cents worth becaus eyou can see this same error even if you supply valid paths.

You will get the same error when the process running the watcher does not have access to the remote share. This will happen if the watcher is in a service running under the System account and the share is created by a user. System does not have access to that share and wont recognize it, you will need to impersonate the user to get access to it.

Karl Strings
  • 1,017
  • 9
  • 22
1

although you can use a FileWatcher over the network, you will have to account for other factors, like disconnection of the network share. If your connection to the share is terminated (maintenance, lag, equipment reset, etc) you will no longer have a valid handle on the share in your filewatcher

Lev
  • 11
  • 1
-2

You can't use directory watches over network shares, this is a limitation of the OS, not of .NET.

Ana Betts
  • 73,868
  • 16
  • 141
  • 209
  • you mean to say, file watcher (because of OS limitation) can not watch over a directory which is physically on some other machine? – Manish Basantani Jun 06 '09 at 20:55
  • I have successfully used filewatcher over a mapped path of the same machine. I mean, filewatcher watching over "\\server\SharedFolder" , while the filles are in "C:\serviceFolder\SharedFolder". This case works fine, the difference here is that both are pointing to same machine, unlike in the example given above. Is that the problem?? – Manish Basantani Jun 06 '09 at 21:00
  • It's trying to give you a hint here - just because you can trick the warning by using mapped drives doesn't mean your watches will actually *work*. – Ana Betts Jun 06 '09 at 21:17
  • 5
    Watchers work just fine on remote shares, mapped drives, etc. so either I don't understand the hint or you are very confused. – Karl Strings Jul 23 '10 at 04:36