C#FileSystemWatcher锁定文件夹 [英] C# FileSystemWatcher lock folder

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

问题描述

我正在尝试使用C#和FileSystemWatcher监视文件夹.一切正常,除了我可以删除我实际上正在观看的文件夹

I'm trying to monitor a folder using C# and FileSystemWatcher. everything works well, except the fact that i can delete the folder i'm actually watching

我以前在C中使用ReadDirectoryChangesW来做到这一点,方法是创建文件夹的句柄并将其锁定,这阻止了用户从该文件夹中删除或重命名该文件夹(我说的是实际受监视的文件夹,而不是它的内容) )

I used to do this in C using ReadDirectoryChangesW, by creating a handle to the folder, and locking it, which prevented delete or rename from the user to that folder (i'm talking about the actual monitored folder, not it's contents)

有什么方法可以锁定该文件夹,以便人们在观看该文件夹时不会将其删除?

Is there any way to lock that folder so people don't delete it while it's being watched?

(请注意,我不想更改该文件夹的权限,因为它可能位于FAT32分区/usb驱动器/etc上,该文件夹不支持权限)

(note that I don't want to change permissions to the folder because it might be on a FAT32 partition/usb drive/etc , which doesn't support permissions)

推荐答案

不确定是否可以选择,但是您可以在所述文件夹中创建一个(临时)文件,并在监视"期间将其保持打开状态.您需要在离开航线后再次对其进行清理. (您甚至可以给它提供hidden属性,这样它就不会向普通"用户显示).

Not sure if that's an option, but you could create a (temporary) file in said folder and keep it open for the duration of the 'watch'. You'll need to clean it up again afterwards off course. (You might even give it the hidden attribute so it doesn't show up to 'normal' users).

不是最好的解决方案,并且在程序崩溃之前,文件会四处乱扔,然后再删除文件...

Not the nicest solution and the file will remain littering around when your program crashes before removing the file...

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

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