Open Files

I'm using the following code to verify if a file is open by any application, it works in some file but the .txt files  and MS word file don't work it tell me that they are free.

What could be

            Dim FS As IO.FileStream = IO.File.Open(FileName, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None)
            FS = Nothing
            open = False ' 'Free file

        Catch ex As IO.IOException
            open = True ' 'Busy File
        Catch ex As Exception
        End Try

Open in new window

Who is Participating?
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.

Applications typically don't lock a file, especially a txt file.
Carlos VillegasFull Stack .NET DeveloperCommented:
Applications typically don't lock a file, especially a txt file.
cano63Author Commented:
And how i can know if a file i open by other application
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Carlos VillegasFull Stack .NET DeveloperCommented:
Hello cano63, by using your code you can know when an application currently has active handler over the file, when strickdd said "Applications typically don't lock a file, especially a txt file." that means the application open the file, reads it to the memory, then closes the file, releasing any handler, so you must understand that in first place, then you can explore another ways to achieve what you want to do.
cano63Author Commented:

But I try to run the code in a notepad and the system tell me that is not used by any one, but if I use a word pefect document it work.

Why is the diference
Carlos VillegasFull Stack .NET DeveloperCommented:
Hello, because windows notepad work in that way (open, read the content and closes it), that behavior is not a rule, all depend in how an application is developed, I think that a word perfect document has a more complex format that a txt file, so is not a good idea to load it to the memory at once.

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
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.