使用 DirectoryInfo 时监视目录中的文件被锁定

发布于 2024-10-19 18:42:30 字数 252 浏览 1 评论 0原文

我一直在监视网络上目录中的文件。我最初使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩发小公举 2024-10-26 18:42:30

您确定创建/更新文件的操作已完成吗?如果没有,该文件将被锁定。

Are you sure that whatever is creating/updating the files is done? If not, the file will be locked.

浪漫人生路 2024-10-26 18:42:30

尽管 FileWatcher 应该在 UNC 共享上工作,但我在这样做时遇到了很多问题。由于您的问题不是时间关键的,所以我会创建一个线程,仅检查文件是否存在,然后循环休眠几秒钟,直到文件消失,此时发送警报。

注意:作为服务运行还要求运行用户在远程共享上拥有网络权限。

更新:刚刚对我们的网络进行了快速测试。客户端运行Windows 7,服务器运行Windows 2008 R2。添加了几个大小文件到共享中。在代码运行时删除文件没有任何问题。即使没有 Thread.Sleep

        bool filesDeleted = false;

        while (!filesDeleted)
        {
            DirectoryInfo di = new DirectoryInfo(@"\\server\share\path\");
            FileInfo[] files = di.GetFiles();

            foreach (var file in files)
            {
                DateTime created = file.CreationTime;
                string fileName = file.Name;

                //Do what every you need to check if the two files are still there
            }

            Thread.Sleep(5000);
        }

        //Send alert

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

        bool filesDeleted = false;

        while (!filesDeleted)
        {
            DirectoryInfo di = new DirectoryInfo(@"\\server\share\path\");
            FileInfo[] files = di.GetFiles();

            foreach (var file in files)
            {
                DateTime created = file.CreationTime;
                string fileName = file.Name;

                //Do what every you need to check if the two files are still there
            }

            Thread.Sleep(5000);
        }

        //Send alert
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文