Advanced file open options in VB

Advanced file open options in VB

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.

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.