使用 DirectoryInfo 时监视目录中的文件被锁定
我一直在监视网络上目录中的文件。我最初使用 FileWatcher 来监视它们。我发现使用 FileWatcher 时文件被锁定,因此我更改了实现以使用 DirectoryInfo 来监视我正在监视的目录中新到达和删除的文件。似乎在使用 DirectoryInfo 时,监视目录中的文件也被锁定,从而防止文件在下载文件后被另一个应用程序删除。我的手表应用程序是一个 Windows 服务。
有人可以告诉我他们是否遇到过问题吗?如果有,您是如何解决的?
谢谢,
I have been monitoring files in a directory on the network. I had initially used FileWatcher to monitor them. I found that the files were being locked while using FileWatcher so I changed my implementation to use DirectoryInfo to watch for newly arrived and deleted files in the directory that I am watching. It seems while using DirectoryInfo also, the files in the watch directory are getting locked thereby preventing the files from being deleted by another application after downloading the files. My watch application is a windows service.
Could anybody tell me if they have faced issues and if they have, how you have been resolve it ?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定创建/更新文件的操作已完成吗?如果没有,该文件将被锁定。
Are you sure that whatever is creating/updating the files is done? If not, the file will be locked.
尽管 FileWatcher 应该在 UNC 共享上工作,但我在这样做时遇到了很多问题。由于您的问题不是时间关键的,所以我会创建一个线程,仅检查文件是否存在,然后循环休眠几秒钟,直到文件消失,此时发送警报。
注意:作为服务运行还要求运行用户在远程共享上拥有网络权限。
更新:刚刚对我们的网络进行了快速测试。客户端运行Windows 7,服务器运行Windows 2008 R2。添加了几个大小文件到共享中。在代码运行时删除文件没有任何问题。即使没有 Thread.Sleep
Even though FileWatcher is supposed to work on UNC shares I've had numerous problems doing so. Since your issue is not time-critical I'd create a Thread that just checks if the files are present, Sleeps a few seconds the loops until the files are gone at which time your alert is sent.
NB: Running as a service requires the running user to have network privileges on the remote share as well.
Update: Just did a quick test on our network. Client running Windows 7 server running Windows 2008 R2. Added several files, both small and large to the share. Did not have any problems deleting the files while the code was running. Even without the Thread.Sleep