FileSystemWatcher锁定文件夹 [英] FileSystemWatcher locks folder

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

问题描述

我在文件夹中监听 FileSystemWatcher .当我尝试重命名此文件夹时(在Windows资源管理器中),我得到

I have a FileSystemWatcher listening on a folder. When I try renaming this folder (in Windows Explorer) I get a

该操作无法完成,因为其中的文件夹或文件位于在另一个程序中打开

The action can't be completed because the folder or a file in it is open in another program

错误.

注释 FileSystemWatcher 调用可以解决此问题.

Commenting out the FileSystemWatcher calls fixes this.

我尝试用一​​个简单的程序来重现错误,但是没有成功,所以我不确定这里发生了什么.有没有人遇到 FileSystemWatcher 这样的事情?如果是这样-可能是什么原因造成的?

I've tried reproducing the error with a simple program, but haven't been successful so I'm not sure what's going on here. Has anyone encountered anything like this with a FileSystemWatcher? If so - what might be causing it?

更多信息:

以下内容似乎足以引起错误:

The following seems to be enough to cause the error:

FileSystemWatcher fsw = new FileSystemWatcher(path);
fsw.Deleted += new FileSystemEventHandler(fsw_Deleted);
fsw.EnableRaisingEvents = true;

可以重命名该文件夹中的文件.和同级文件.我什至尝试将 return; 作为 fsw_Deleted 的第一行.还是没用.如未关闭Visual Studio,然后删除obj和bin,然后再次运行.我什至尝试不在VS中运行exe文件.仍然-如果没有运行,则没有锁定,如果没有,则锁定.

I can rename files in that folder. And sibling files. I even tried putting a return; as the first line in fsw_Deleted. Still didn't work. As didn't closing Visual Studio, and deleting obj and bin, and running again. And I even tried running the exe file not within VS. Still - no lock if it's not running, Locked if it is.

推荐答案

我发现了造成这种情况的原因.在第一个子目录中有第二个 FileSystemWatcher -不允许重命名第一个.

I've discovered what caused this. There was a second FileSystemWatcher - on a sub directory of the first - which didn't allow renaming the first.

(不过,我仍然感到惊讶. FileSystemWatcher 应该是不可见的".)

(I'm still surprised, though. A FileSystemWatcher should be "invisible".)

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

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