简体   繁体   中英

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

是否可以在Windows中锁定目录,以确保在锁定期间没有其他进程正在读取或修改目录内的文件,同时允许带锁的进程修改和移动文件和目录本身自由吗?

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM