Files and Folders

Advanced open options

You might want, at some stage more control over what happens when opening a file. For example, you might want to prevent other applications from writing to it, while you were reading it, or prevent them from reading the file at all! This is all done using different options in the Open statement, and is actually quite simple. To use these, you simply use one of the following keywords after the As Input/Output part of the statement. For example,

Open File For Method LockOptions As nFileNum

The different lock options are explained below:

Shared Lets other applications (and the OS) do what it wants with the file, even if you are modifying it.
Lock Read Prevents other applications from reading the file, until you close it.
Lock Write Prevents other applications from writing to the file, until you close it.
Lock Read Write Prevents other applications from writing to the file or reading it, until you close it.

The last three options also prevent the open file from being deleted, and your program getting a nasty shock when it tries to write to/read from it again!

So,

Open App.Path & "\test.txt" For Input Lock Read Write As nFileNum

opens text.txt for Input, and prevents other applications from reading or writing to it, and stops it from being deleted.

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Linux is only free if your time has no value” - Jamie Zawinski