FileSystemWatcher Fails to access network drive

吃可爱长大的小学妹 提交于 2019-12-25 02:40:28

问题


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?


回答1:


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.




回答2:


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.




回答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?




回答4:


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.




回答5:


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




回答6:


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




回答7:


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



来源:https://stackoverflow.com/questions/8326302/reading-unc-path-with-filesystemwatcher

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