Results 1 to 3 of 3

Thread: How can I tell if a file is in use?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Location
    NC, USA
    Posts
    102

    Question

    I am working on a pretty cool little project to add to my existing application that will check the web for updates to my program. If files on the client are older than any of the files on the web server it will allow them to automatically update them. The only problem is...

    If it is copying a file that is in use (like a system dll) then I can not copy it using normal methods. I will have to use the registry method. That is fine. What I need though is to know when a file is in use so I can invoke the correct method of updating it.

    TIA,
    Richard

  2. #2
    Guest
    Try this:


    Code:
    Private Declare Function lOpen Lib "kernel32" Alias "_lopen" (ByVal lpPathName As String, ByVal iReadWrite As Long) As Long
    Private Declare Function lClose Lib "kernel32" Alias "_lclose" (ByVal hFile As Long) As Long
    
    
    Function IsFileAlreadyOpen(FileName As String) As Boolean
    
     Dim hFile As Long
     Dim lastErr As Long
    
     ' Initialize file handle And Error variable.
     hFile = -1
     lastErr = 0
    
     ' Open for for read And exclusive sharing.
     hFile = lOpen(FileName, &H10)
    
     ' If we couldn't Open the file, Get the last error.
     If hFile = -1 Then
        lastErr = Err.LastDllError
        Else
        ' Make sure we Close the file On success.
        lClose (hFile)
     End If
    
     ' Check for sharing violation error.
     sFileAlreadyOpen = (hFile = 1) And (lastErr = 32)
    
    End Function

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Location
    NC, USA
    Posts
    102

    Talking

    Cool, thanks for the help. I will check it out right now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width