服务中的C#FileSystemWatcher [英] c# FileSystemWatcher in a service

查看:139
本文介绍了服务中的C#FileSystemWatcher的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务,该服务实现FileSystemWatcher,以监视对远程计算机上一个文件的更改. 我使用UNC路径作为文件的路径,但是,事件日志中出现错误,指出该路径无效.我确认该路径实际上是有效的.另外,在服务中,我尝试使用有效的用户名和密码尝试所有不同的帐户类型.我还确认了我的代码是正确的,因为我使用FileSystemWatcher中的本地路径成功对其进行了测试.那么,我想念的是什么?我在其他站点上阅读了一些文章如何成功地使用UNC路径来使用FileSystemWatcher监视远程计算机上的文件,但是它们并没有提供有关如何完成文件的任何真正见解.

I have a service that implements FileSystemWatcher to watch for changes to one file on a remote computer. I use a UNC path for the path to the file but, I get an error in the event log saying that the path is invalid. I confirmed that the path is actually valid. Also, in the service I tried ALL the different account types with a valid username and password. I also confirmed that my code is correct as I tested it successfully using a local path in the FileSystemWatcher. So, what am I missing? I read on other sites how some have successfully used UNC paths to monitor files on remote computers using FileSystemWatcher but, they didn''t provide any real insight on how it was accomplished.

Any practical suggestions on how this is done would be greatly appreciated.

推荐答案

UNC路径可能是有效的,但对于尝试访问它的帐户必须是有效的,另外,UNC路径可能会在短时间内不可用,从而导致FileSystemWatcher报告无效路径.

所以请牢记这一点;
1.您用来测试UNC路径的帐户,该帐户与该服务使用的帐户相同吗?如果不是,则必须为其他帐户,否则您要测试的帐户可能与运行该帐户的帐户没有相同的访问网络资源的权限.

2.当UNC路径由于任何原因而变得不可用时,您需要捕获该错误并在以后重试,可以显示如何执行此操作的文章为
A UNC path may be valid BUT is needs to be valid for the account trying to access it, also a UNC path may become unavailable for a short period which causes the FileSystemWatcher to report an invalid path.

So with this in mind;
1. the account you test the UNC path with, is this the same as used by the service? If not it must be otherwise the account you are testing with may not have the same permissions to access network resources as the account the service runs under.

2. When a UNC path becomes unavailable for wahtever reason you need to catch the error and retry at a later date, an article that can show how to do this is FileSystemWatcher - Pure Chaos (Part 1 of 2)[^]

Remember when using a UNC path it may or may not be valid and you really need to know why this may be, either due to account permissions or the server going down.


感谢所有答复和建议的解决方案.

我发现我需要共享服务试图访问的确切文件夹,而不是简单地将用户帐户添加到服务器并使用完整的unc路径.

换句话说,这是我刚开始做的事情:
1.)将用户帐户添加到服务器中
2.)在文件监视程序中使用FULL UNC路径:
\\服务器名称\ drive_share \ folder1 \ folder2 \ TARGET_Folder \
这没用.

注意: TARGET_Folder 是服务需要访问的文件夹
在Windows2008服务器上.

适用于我的解决方案如下:
1.)将用户帐户添加到服务器中
2.)共享服务需要访问的确切文件夹(TARGET_Folder)
3.)直接使用unc路径访问文件监视程序中的共享文件夹:
\\服务器名称\ TARGET_Folder
这个解决方案对我有用.
Thanks for all the replies and the proposed solution.

I found that I needed to share the exact folder that the service was attempting to access instead of simply adding the user account to the server and using a full unc path.

In other words, here is what I was doing at first:
1.) Add the user account to the server
2.) Use the FULL unc path in the filewatcher:
\\servername\drive_share\folder1\folder2\TARGET_Folder\
THIS DID NOT WORK.

Note: TARGET_Folder is the folder that the service needs to access
on a Windows2008 server.

The solution that worked for me is as follows:
1.) Add the user account to the server
2.) Share the exact folder (TARGET_Folder) the service needs to access
3.)Use the unc path directly to the shared folder in the filewatcher:
\\servername\TARGET_Folder
This solution worked for me.


这篇关于服务中的C#FileSystemWatcher的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆