Results 1 to 8 of 8

Thread: dtecting if a file is presently open

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2002
    Posts
    586

    Cool dtecting if a file is presently open

    Greetings,

    OK, here;'s the pseudocode. Are there command or properties to make it a reality?

    ' open a given file
    ' if the file is already opened by another user then
    'MsgBox "Another user is presently using this file.
    Data corruption may occur."
    'end if

    For example is there some file property I can evaluate to determine if the file is presently open?

    Thank you,
    Jim

  2. #2
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    just open the file. If you get an error #55, then it is already open and you will not be allowed to open it so "data corruption" is not an issue as you can't write to a file that someone else has open.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2002
    Posts
    586
    Originally posted by phinds
    just open the file. If you get an error #55, then it is already open and you will not be allowed to open it so "data corruption" is not an issue as you can't write to a file that someone else has open.
    What if they are using Terminal Services?

  4. #4
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    Delete the file , and if you can delete it, then it's not open

  5. #5
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    Actually, a safer way, would be to rename the file... and if you can rename it, then it's not open

  6. #6
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    VB Code:
    1. 'Determine whether a file is already open or not
    2. Private Declare Function lOpen Lib "kernel32" Alias "_lopen" (ByVal lpPathName As String, ByVal iReadWrite As Long) As Long
    3. Private Declare Function lClose Lib "kernel32" Alias "_lclose" (ByVal hFile As Long) As Long
    4. Private Function IsFileAlreadyOpen(FileName As String) As Boolean
    5.     Dim hFile As Long
    6.     Dim lastErr As Long
    7.     ' Initialize file handle and error variable.
    8.     hFile = -1
    9.     lastErr = 0
    10.     ' Open for for read and exclusive sharing.
    11.     hFile = lOpen(FileName, &H10)
    12.     ' If we couldn't open the file, get the last error.
    13.     If hFile = -1 Then
    14.         lastErr = Err.LastDllError
    15.     Else
    16.         ' Make sure we close the file on success.
    17.         lClose (hFile)
    18.     End If
    19.     ' Check for sharing violation error.
    20.     sFileAlreadyOpen = (hFile = -1) And (lastErr = 32)
    21. End Function
    22. Private Sub Form_Load()
    23.     'example by Matthew Gates ([email protected])
    24.     MsgBox IsFileAlreadyOpen("c:\autoexec.bat")
    25. End Sub

  7. #7
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    I might be wrong, but if you open the file with this flag: FILE_SHARE_READ_WRITE, then it means that the file can be opened in the same time with another program ? So if that flag is used then the second Open will not return an error.

  8. #8
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    JimMuglia


    If you open the file exclusive no sharing, you will either open the file or get an error if it is already open.

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