[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Check if file is in use

Posted on 2004-12-01
1
Medium Priority
?
248 Views
Last Modified: 2012-05-05
Hello,

I want to check in my vb program if a specific file is in use. Only when the file isn't in use anymore, my program may continue?

Can somebody help?

Thnx!
0
Comment
Question by:Q-Tip
1 Comment
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 12717447
What you can do is attempt to open the file with exclusive read write access.  If the attempt fails, it must be in use by another process:

Option Explicit

Private Sub Command1_Click()
    If isFileInUse(Text1.Text) Then
        Label1.Caption = "File In Use"
    Else
        Label1.Caption = "File Available"
    End If
End Sub

Private Function isFileInUse(ByVal filename As String) As Boolean
    On Error GoTo openError
   
    isFileInUse = False ' assume it isn't locked
    If Dir(filename) <> "" Then
        ' attempt to get exclusive access to the file
        ' if exclusive access fails, we jump to openError:
        ' and return true
        Open filename For Binary Lock Read Write As #1
        Close #1
    Else
        MsgBox filename, vbCritical, "File Not Found"
    End If
    Exit Function

openError:
isFileInUse = True
End Function
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question