rlsokoloff:
The best way is to try to open the file for exclusive access.
Main Topics
Browse All TopicsHow can I test if a file is being used by another process?
More specificly, if I have an EXCEL file, e.g. "C:\TheFile.xls", how can I test if it's being used by another process?
If I try to stream data to such a file I get an IOException, but I would like to test if the file is being used before attempting to stream data.
Think all I need is just a line or two of VB.NET code.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Thanks youfor the responses,
However, as I mentioned, I want to test for "used by another process" before doing any additional work or manipualtion of the file and I want to do this without raising an exception.
I do get an IOException if I try to stream the file as a BLOB into a database field while it had been inadvertently left open, and I want to test before attempting to stream the data
Isn't there some off-the-shelf function where I can just enter the path of the file as an argument and it will return FALSE if it is NOT being used by another process and TRUE if it is being used.
rlsokoloff:
I haven't run into any programming or scripting languages that have such a function built in. Every one I've run into does it as I've suggested. Why would you want to avoid raising an exception? They're the standard method for detecting failed conditions. Your code should have TRY / CATCH blocks for handling them. The code example in the link below will do what you want.
http://dotnet-snippets.com
AielloJ
u can use Unlocker to do this. just right click on the file and click on unlock if the file is in use by some program u will able to see the program name and if file is not in use u will get the proper message about it.
get the software from here
http://ccollomb.free.fr/un
Regarding Try...Catch....Finally comment,
I do use these blocks. However, my application has a method that performs a lot of steps (A, B, C, D.....etc) BEFORE it attempts to stream the file as a BLOB into an image field in the database. Streaming this file is the last step in the method and that's where I would throw the IOException "Cannot access file.....used by another process." This exception is thrown if I inadvertently left the file open.
I would like to test early in the method to assure the file is NOT in use before attempting to perform steps A, B, D, E.
Business Accounts
Answer for Membership
by: tipsybroomPosted on 2009-08-12 at 15:01:01ID: 25083528
You can try open the file, and if there is an exception you know that someone else has that file open. So you trap the error and either: try again, or: tell the user that the file is currently in use.