Lock file for writing/deleting while allowing any process to read

后端 未结 4 809
再見小時候
再見小時候 2020-12-28 07:58

I am developing an application in C# (.NET), and am having trouble dealing with file locking.

  • My main application (A) needs read/write access to a certain fil
4条回答
  •  北海茫月
    2020-12-28 08:55

    Most of the time, a locking of the file is not to prevent user deleting the file, but inform user running another instance of the application that the file is "in use" by another user. This is expecially useful if multiple users are opening r/w a file into a shared folder. In such scenario, instead of locking the file at filesystem level, would be much more easier to use a "lock file" generated when Appication (A) opens the file. Thus, any other application, would notice that a lock file exist (you can name it using the same filename but different extension), and also inside the locking file you can write who and when someone have aquired the lock. Application (B) can now respond to user... "The file appear to be under modification by user xxx from machine yyy, do you really want to load it ?"

    Of course, the application must remove the lock file when the application file is no longer in use or when the application terminates. In the "unfortunate" case that a crash leave the lock on filesystem, user can just respond yes to the warning request, or can manually delete it to free the lock.

    Hope this helps,

    Paolo Marani

提交回复
热议问题