Why is Windows not warning about file in use for certain programs?

Solution 1:

Not all programs lock files when opened for editing

  • Windows has two types of file locks, shared locks and exclusive locks. Shared locks allow other programs to read the file but deny write access to other programs. Exclusive locks prevent other programs from accessing the file altogether. A program may use either type of lock as required.

  • Programs can "open" files for editing but not lock them. What really happens here is that the file is loaded into a buffer in memory and the status of the file is not checked until it is operated on again. A file "open" in this fashion is not really in use and can be manipulated freely by other programs or the system itself, which allows you to continue editing the non-existent file and save it into a new file.

  • Notepad does not lock the file opened, which means that the file can be deleted while it is open in Notepad and the program will not notice that the file is deleted until it tries to open it again. Similarly, Paint doesn't lock the file and only notices that the file is deleted when it tries to open or save it.

  • Word, on the other hand, locks documents when it opens them (edit: from my research, it seems the lock is exclusive only when Word is reading from or writing to the file, and is shared otherwise). As a result, you cannot delete files that are currently open in Word.

Opening a file without locking it can be risky

  • Although it is simple to just load the file into memory and not touch it until necessary, doing so without locking it carries the risk of a race condition where one program may edit the file without the changes being reflected in another problem. That other program (which might very well be Notepad or Paint) will not see the changes, and they will be lost when you save the file in that program.

  • This risk of race conditions is the reason Microsoft Word (and other Office programs) lock files when they open them. On the other hand, as an example of an alternative solution, Notepad++ doesn't lock opened files so that other programs can continue to manipulate them. However, it will warn the user if it detects that the file has changed or is deleted, asking the user whether to reload the file from disk or close it, respectively—both of these actions would discard the user's changes.