开发者

How to get a process exclusive lock on a folder in Windows?

Is it possible to lock a directory in Windows so as to ensure that no other process is reading or modifying files inside the directory for the duration of the lock, while at the same time allowing the process with the lock to modify and mo开发者_开发知识库ve files and directory itself freely?


This is not a real answer, but as a workaround:

Move the directory to a subdirectory specific to your application, which is on the same volume.

Advantages:

  • Prevents users and other programs from modifying the file at the old location, as the files will no longer be there
  • Importantly, will fail if a process already has a file open within that directory, thus ensuring that the "acquired" lock is indeed "exclusive"

Disadvantages:

  • It's a hack
  • The software will need to be adapted to work with the directory at a different path than where it was initially
  • Users and programs attempting to access the files will encounter unusual behavior or errors ("Path not found" instead of "Access denied")
  • Does not protect against programs that may poke into your application-specific subdirectory
  • Will leave the directory "locked" (moved to a location the user probably can't find) if your program crashes while the "lock" is "held"
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜