C# FileSystemWatcher 无法访问网络驱动器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/960318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:17:51  来源:igfitidea点击:

FileSystemWatcher Fails to access network drive

c#windows-servicesfilesystemwatcher

提问by Manish Basantani

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:

我正在尝试使用 Windows 服务在某个服务器路径上运行文件观察器。我正在使用我的 Windows 登录凭据来运行该服务,并且能够从我的登录中访问这个“someServerPath”。但是当我从 FileSystemWatcher 这样做时,它会抛出:

The directory name \someServerPath is invalid" exception.

目录名 \someServerPath 无效”异常。

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?

任何人都可以帮我解决这个问题吗?

采纳答案by Lance McNearney

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

我有使用 FileSystemWatcher 对象监视 UNC 路径的项目,没有任何问题。

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.

通过查看您的代码示例,我的猜测可能是您将观察者指向服务器的根共享(//servername/),这可能不是有效的文件系统共享?我知道它会在 Windows 资源管理器中返回打印机、计划任务等内容。

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.

尝试将观察者指向根目录下的共享 - 如果您在服务器上具有远程管理权限,则类似 //servername/c$/ 的内容将是一个很好的测试示例。

回答by John Weldon

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"

将名称指定为 @"\\someServerPath"

The \ is being escaped as a single \

\ 被转义为单个 \

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

当您使用 @ 符号作为字符串前缀时,它不会处理转义序列。

回答by Neil N

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

您是否有任何特殊原因不在具有您要观看的目录的物理机上运行 FileSystemWatcher?

回答by Ana Betts

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

您不能在网络共享上使用目录监视,这是操作系统的限制,而不是 .NET 的限制。

回答by Karl Strings

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.

当运行观察者的进程无权访问远程共享时,您将收到相同的错误。如果观察者在系统帐户下运行的服务中并且共享是由用户创建的,则会发生这种情况。系统无权访问该共享并且无法识别它,您需要模拟用户才能访问它。

回答by Lev

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

尽管您可以通过网络使用 FileWatcher,但您必须考虑其他因素,例如网络共享断开连接。如果您与共享的连接终止(维护、延迟、设备重置等),您将不再拥有文件观察器中共享的有效句柄

回答by Rhyous

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.

我刚刚被问到这个关于作为服务运行的 FileSystemWatcher 代码的问题,问题是权限。我搜索并找到了这个问题和答案,但不幸的是,这里的答案都没有解决问题。无论如何,我刚刚解决了它,所以我想我会在这里为下一个搜索并找到这个问题的人提供解决方案。

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

该驱动器被映射为登录用户,但该服务作为LocalSystem运行。LocalSystem 是一个不同的帐户,无权访问用户映射的驱动器。

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.
  1. 首先进行身份验证(我使用C# 类与凭据建立网络连接
  2. 以有权访问共享的用户身份运行您的服务。

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

您可以使用 LocalSystem 命令提示符测试 LocalSystem 身份验证,请参阅如何打开作为本地系统运行的命令提示符?