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

file managementwindows

When you open a text file in Notepad on Windows, you can move the source file to the Recycle Bin or even permanently delete it. The same is true for WordPad. Why is it that Windows doesn't give you a warning, complaining about file being open in another program?

However, if the same file is open in Word, and you try to moved the source file to the recycle bin or delete it permanently, Windows does give you a warning. The message itself can be a warning type of message, telling you that some (often unknwon) program has the file open. In case of Word 2013 on Windows 8.1 this is an info message, telling you exactly what program (Word) has the file open.

notepad del file
notepad open file

Why is that? Why is it that you can A) delete the source file while it's open in a program, and B) continue working on the document after the file has been deleted? Why is the behavior different in Notepad and WordPad from that of Word?

word del file
word open file

The first case behavior exists in Paint too. You can delete a bitmap file while it's open in Paint and continue working. But when you try to save it you get prompted for file location to save it to. With Notepad, it directly saves it to the same location that the original file was stored in and gives it the same name. Notepad doesn't prompt you for that.

I have not tested this on Windows 7, Vista, or XP. However, I would expect the behavior to be exactly the same, and so the question would probably apply to those systems as well.

Best Answer

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.

Related Question