Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileSystemWatcher Fails to access network drive

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?

like image 673
Manish Basantani Avatar asked Jun 06 '09 19:06

Manish Basantani


5 Answers

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.

like image 186
Lance McNearney Avatar answered Nov 16 '22 16:11

Lance McNearney


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.

like image 5
John Weldon Avatar answered Nov 16 '22 18:11

John Weldon


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?

like image 3
Rhyous Avatar answered Nov 16 '22 18:11

Rhyous


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.

like image 2
Karl Strings Avatar answered Nov 16 '22 16:11

Karl Strings


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

like image 1
Lev Avatar answered Nov 16 '22 17:11

Lev