FileSystemWatcher 无法访问网络驱动器
我正在尝试使用 Windows 服务在某些服务器路径上运行文件观察器。 我正在使用我的 Windows 登录凭据来运行该服务,并且能够从我的登录名访问此“someServerPath”。 但是当我从 FileSystemWatcher 执行此操作时,它会抛出:
目录名称 \someServerPath 无效”异常。
var fileWatcher = new FileSystemWatcher(GetServerPath())
{
NotifyFilter=(NotifyFilters.LastWrite|NotifyFilters.FileName),
EnableRaisingEvents=true,
IncludeSubdirectories=true
};
public static string GetServerPath()
{
return string.Format(@"\\{0}", FileServer1);
}
任何人都可以帮我解决这个问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我有使用 FileSystemWatcher 对象监视 UNC 路径的项目,没有任何问题。
通过查看您的代码示例,我的猜测可能是您将观察者指向服务器的根共享(//servername/),这可能不是有效的文件系统共享? 我知道它会在 Windows 资源管理器中返回打印机、计划任务等内容。
尝试将观察者指向根目录下的共享 - 如果您在服务器上拥有远程管理权限,则类似 //servername/c$/ 的内容将是一个很好的测试示例。
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.
关于更新的问题,我同意您可能需要指定有效的共享,而不仅仅是远程服务器名称。
[更新] 修复了之前有关此异常的问题:
将名称指定为
@"\\someServerPath"
\ 被转义为单个 \
当您在字符串中添加 @ 符号前缀时,它不会不处理转义序列。
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.
我刚刚被问到这个关于作为服务运行的 FileSystemWatcher 代码的问题,问题是权限。 我搜索并找到了这个问题和答案,但不幸的是这里的答案都没有解决问题。 不管怎样,我刚刚解决了它,所以我想我会在这里为下一个搜索并找到这个问题的人提供解决方案。
驱动器被映射为登录用户,但服务作为本地系统运行。 LocalSystem 是一个不同的帐户,无权访问用户映射的驱动器。
修复方法是:
您可以使用 LocalSystem 命令提示符测试 LocalSystem 身份验证,请参阅 如何打开作为本地系统运行的命令提示符?
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:
You can test LocalSystem authentication by using a LocalSystem command prompt, see How to open a command prompt running as Local System?
尽管这个问题已经得到解答,但我想我应该投入两美分,因为即使您提供有效的路径,您也可以看到同样的错误。
当运行观察程序的进程无权访问远程共享时,您将收到相同的错误。 如果观察程序位于系统帐户下运行的服务中并且共享由用户创建,则会发生这种情况。 系统无权访问该共享并且无法识别它,您将需要模拟用户才能访问它。
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.
尽管您可以通过网络使用 FileWatcher,但您必须考虑其他因素,例如网络共享的断开连接。 如果您与共享的连接终止(维护、延迟、设备重置等),您将不再在文件监视程序中拥有该共享的有效句柄
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
您无法通过网络共享使用目录监视,这是操作系统的限制,而不是 .NET 的限制。
You can't use directory watches over network shares, this is a limitation of the OS, not of .NET.