开发者

C# FileSystemWatcher lock folder

开发者 https://www.devze.com 2023-01-02 01:16 出处:网络
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

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

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?

(note that I don't want to change permissions to the folder because it might be on a FAT32 partition/usb drive/etc , which d开发者_如何转开发oesn't support permissions)


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...

0

精彩评论

暂无评论...
验证码 换一张
取 消