Results 1 to 8 of 8

Thread: How to find if a file is in use?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Posts
    91

    Question How to find if a file is in use?

    Hello,

    Can any one tell me how do I find if a file is in use?

    Thanks.

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    EnumWindows might help you if you wish to find a seperate process thats running.
    enumprocess api
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Posts
    91
    Nope 'enumprocess' would not help.

    The reason being 'enumprocess' would allow me to enumerate the currently running process list. But, how do I know which of the documents is being accessed by them.

    Isn't there any API do check if any file is in use?


    Thanks.

  4. #4
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    This out. It should help


    Has someone helped you? Then you can Rate their helpful post.

  5. #5
    Addicted Member mandark's Avatar
    Join Date
    Oct 2002
    Posts
    188
    I am working on a compression program, where I would like to make sure that none of the files that I want to compress are in use.

    But, is this the best possible option.

    As there may be over a thousand files that I need to compress.

    If I have to check each file by opening in a binary mode, wouldn't it make it slow??


  6. #6
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Well it opens the file but it doesn't input it so it shouldn't be TOO slow. But I don't know any other way so I guess you'll have to settle for this, eh?


    Has someone helped you? Then you can Rate their helpful post.

  7. #7
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    San Jose, Ca. - USA
    Posts
    302
    I found this in API-Guide

    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.  
    5. Private Function IsFileAlreadyOpen(FileName As String) As Boolean
    6.     Dim hFile As Long
    7.     Dim lastErr As Long
    8.     ' Initialize file handle and error variable.
    9.     hFile = -1
    10.     lastErr = 0
    11.     ' Open for for read and exclusive sharing.
    12.     hFile = lOpen(FileName, &H10)
    13.     ' If we couldn't open the file, get the last error.
    14.     If hFile = -1 Then
    15.         lastErr = Err.LastDllError
    16.     Else
    17.         ' Make sure we close the file on success.
    18.         lClose (hFile)
    19.     End If
    20.     ' Check for sharing violation error.
    21.     sFileAlreadyOpen = (hFile = -1) And (lastErr = 32)
    22. End Function
    23.  
    24. Private Sub Form_Load()
    25.     'example by Matthew Gates ([email protected])
    26.     MsgBox IsFileAlreadyOpen("c:\autoexec.bat")
    27. End Sub

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Posts
    91
    Thanks guys.


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