Discussion Locked

Open Files

0 helpful
6 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
In VB, how do I test to see if a file is in use?

6 replies

by mike (from canada)

add this line first:

On error resume next

backup the file before doing this:

open for output as #1

now if there is an error, then you know the file is being used. I think the same works with:

open for input as #1

the error message will be similar to "File already open". check the error codes and see which error number the message corresponds to.

Now add:

If Err = number% then msgbox "File in use"

make number% equal to the error code.

by salexru2000

I would not test at all. Better to use available error detection techniques after you acess file.

In older VB versions it will be like

on error resume next
...
access file statement
if err.number<>0 then
-- here you can analyse the exact cause and do whetever you want
endif

If you need same in VB.Net - use try-catch blocks - for me it was always working perfectly.

Hope this helps
Rgds

This discussion is closed to new replies.