Open .txt file for exclusive use
-
In a filewatcher I want to test to see if a file is complete. I tried this: File.Open("c:\test.txt", FileMode.Open, FileAccess.Read, FileShare.None) I don't get an exception when the file is open with Notepad. How can I detect the file is opened by another program? Thanks
-
In a filewatcher I want to test to see if a file is complete. I tried this: File.Open("c:\test.txt", FileMode.Open, FileAccess.Read, FileShare.None) I don't get an exception when the file is open with Notepad. How can I detect the file is opened by another program? Thanks
Notepad doesn't lock a file open. It opens a file for Shared access, reads the file and closes it immediately. When it goes to save the file, it opens the file for DenyShareAll, writes it out, then closes it.
dBrong wrote:
How can I detect the file is opened by another program?
That depends on the behavior other program, as you've already found out.
dBrong wrote:
How can I detect the file is opened by another program?
Reliably, for any application?? You can't.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007