views:

1512

answers:

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?

+5  A: 

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
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
Ofcourse i am doing the same.Also, i am using @"\\path" before the variable which returns this path.
Amby
Updated my code, hope i get a better solution now.
Amby
A: 

Is there any particular reason you aren't running the FileSystemWatcher on the physical machine that has the directory you want to watch?

Neil N
Its a windows service that run on a mail server and reads files from different location over network.
Amby
edit: main* server
Amby
cant you have the other servers "send" thier files to the main one?
Neil N
Oh come on man!!. the reason we go for different servers because we want to delegate different responsibilities and level of access to each. Why would i duplicate data across different servers?. Anyhow, its getting to be a different conversation altogether. Thanks for the interest.
Amby
Ya I understand it's not ideal to have the service replicated, but in this case, I think its a better option. You could potentially have a "master" service on the main server that is the permissions/security layer that handles all incoming files
Neil N
A: 

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

Paul Betts
you mean to say, file watcher (because of OS limitation) can not watch over a directory which is physically on some other machine?
Amby
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??
Amby
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*.
Paul Betts
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
+3  A: 

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
Thanks. This appears to be the problem here.
Amby
A: 

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
A: 

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