How to dynamically determine if a file is opened by another process

I have a file that I access in my VS app and want to be able to determine if this file is opened by another process before I attempt any file writing operations. An example would be nice.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

There are multiple access flags that can be used on files if another process opens the file with exclusive access you can't access the file. If the process opens the file with shared read access you can still access the file for reading. To determine if the process has the file open and they don't specify the shared write access flag then you catch an IO exception using Try Catch.


Open in new window

atomicgs12Author Commented:
I'm looking for example code. Not theoretically conversation.
Public Function FileIsLocked(ByVal strFullFileName As String) As Boolean
Dim blnReturn As Boolean = False
Dim fs As System.IO.FileStream

    fs = System.IO.File.Open(strFullFileName, IO.FileMode.OpenOrCreate, IO.FileAccess.Read, IO.FileShare.None)
Catch ex As System.IO.IOException
    blnReturn = True
End Try

Return blnReturn
End Function


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Please keep in mind following things:

1.) Opening a file for reading can have some serious side effects in some cases, if another application wants to write to this file. If this is a problem depends on the file.
For example if you open the Outlook .ost file for reading, Outlook will throw an error when starting. The same goes for Access databases.
If you open an SQL Server database for reading and a user wants to connect to the database, the SQL Server thinks it is a disk error and will dismount the database automatically, so dont do this with your main database.

2.) If your check determines that your file is not locked, that does not mean, that opening it will not cause an error, because it could be locked 2 milliseconds later.
If you want to use the file, simply open it for reading and handle the Exception, if it goes wrong. This is the most common way.

       fs = System.IO.File.Open(strFullFileName, IO.FileMode.OpenOrCreate, IO.FileAccess.Read, IO.FileShare.None)
    'Use your file here
Catch ex As Exception
       MsgBox("Something went terribly wrong using the file:" & vbCrLf & vbCrLf & ex.toString)
End Try

3.) There can be several reasons why you cant access a file. The file is in use is only one reason. Another could be access denied because of missing rights, the file is not available because you dont have network connection, the file is damaged because of a hardware fault/problem with the file system, it is possible that the file does not exists and so on. Do you only want to check for "file in use" or for all of this cases.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.