Check if a file is in use VB6-VBForums
Results 1 to 9 of 9

Thread: Check if a file is in use VB6

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    528

    Check if a file is in use VB6

    I need a way to check if a file is in use or not by another application. at the moment I am using:
    Code:
    oFile.Move (oFile.Path)
    If Err = 70 Then
    'the file is in use
    Else 
    'the file is not in use
    End if
    This works but it is slow. Basically it tries to cut and paste the file into the directory it's actually in, so really the file goes nowhere, but if it is locked by another process then it will chuck out error 70 and then we know that it's in use.
    I know that this is a crude way of doing it, so, is there another way to check if a file is in use which is alot faster?

  2. #2
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,111

    Re: Check if a file is in use VB6

    You coult try opening the file for for write access and catch the error if its opened already
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

  3. #3
    Fanatic Member Mxjerrett's Avatar
    Join Date
    Apr 2006
    Location
    Oklahoma
    Posts
    939

    Re: Check if a file is in use VB6


    If a post has been helpful please rate it.
    If your question has been answered, pull down the tread tools and mark it as resolved.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    528

    Re: Check if a file is in use VB6

    Thankyou Mxjerrett
    This method only takes a fraction of the time

  5. #5
    New Member
    Join Date
    Aug 2010
    Posts
    1

    Re: Check if a file is in use VB6

    But if the file is on a cd, can i still use the same method?? I want to check if a file on a CD is in use.. pliz help..

  6. #6
    Addicted Member
    Join Date
    Jun 2009
    Location
    C:\Windows\SysWOW64\
    Posts
    201

    Re: Check if a file is in use VB6

    Yes, it should work for CDs, too.
    Last edited by Cube8; Aug 18th, 2010 at 02:16 AM.


  7. #7
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Check if a file is in use VB6

    Quote Originally Posted by Scranda View Post
    But if the file is on a cd, can i still use the same method?? I want to check if a file on a CD is in use.. pliz help..
    Welcome to the Forum.

    It is general practice to start your own thread when you have a question instead of posting in one started by another member. If another one is related post a link to it in your own. Also, when posting (solutions) to existing threads, do check the date so as not to post/revive old/dead threads. This one is 2 years old. In fact, it was it's second birthday 2 days ago

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Posts
    11,409

    Re: Check if a file is in use VB6

    You really don't need all that code making API calls:
    Code:
        Dim intFile As Integer
        
        intFile = FreeFile(0)
        On Error Resume Next
        Open "example.txt" For Input Lock Read Write As #intFile
        If Err Then
            Label1.Caption = "In Use!"
        Else
            Label1.Caption = "Not In Use!"
        End If
        Close #intFile
    Attached Files Attached Files

  9. #9
    Addicted Member
    Join Date
    Jun 2009
    Location
    C:\Windows\SysWOW64\
    Posts
    201

    Re: Check if a file is in use VB6

    There is a small flaw in this method.
    What if the user doesn't have read/write permissions on the file? A "permisssion denied" error will be thrown.
    I don't remember what error occurs if it is already locked (assuming the user has read/write permissions), but if these 2 errors have different numbers, maybe you could do a check.


Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.