Check if File is in Use
-
Hi Guys. How can I check if a file is being used by another process? I have tried the following but it doesn't seem to work though.
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
If I open the .txt file in NotePad and then run the app it opens the file. What I need to do is for my app to throw an exception that tells me that the
file being used by another process
, or something like that. Anybody have any idea? ThanksExcellence is doing ordinary things extraordinarily well.
-
Hi Guys. How can I check if a file is being used by another process? I have tried the following but it doesn't seem to work though.
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
If I open the .txt file in NotePad and then run the app it opens the file. What I need to do is for my app to throw an exception that tells me that the
file being used by another process
, or something like that. Anybody have any idea? ThanksExcellence is doing ordinary things extraordinarily well.
I'm not sure you can. Notepad Opens the file, reads the content, and closes it again. It may never touch the file again, so how is the OS to know the file is "in use"? If you think about it, most well behaved applications do much the same - file handles are a scarce resource, so you don't hang onto them unless you really must.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
-
Hi Guys. How can I check if a file is being used by another process? I have tried the following but it doesn't seem to work though.
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
If I open the .txt file in NotePad and then run the app it opens the file. What I need to do is for my app to throw an exception that tells me that the
file being used by another process
, or something like that. Anybody have any idea? ThanksExcellence is doing ordinary things extraordinarily well.
There's no way to tell. Notepad doesn't hold the file open while you are viewing it. It's opened, read, and closed before you even see it on screen. Notepad also doesn't block other applications from reading the file at the same time it is, so this code will fail to detect Notepad using the file in all cases.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Hi Guys. How can I check if a file is being used by another process? I have tried the following but it doesn't seem to work though.
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
If I open the .txt file in NotePad and then run the app it opens the file. What I need to do is for my app to throw an exception that tells me that the
file being used by another process
, or something like that. Anybody have any idea? ThanksExcellence is doing ordinary things extraordinarily well.
Try opening this file in MS Word. It locks the file when you open it. Then when you open file, test your application.
Don't forget to rate answer, that helped you. It will allow other people find their answers faster.