Results 1 to 5 of 5

Thread: file locked

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2020
    Posts
    29

    file locked

    Hi all im trying to check if a file is locked.
    so we have a network excel file that many people edit im using closed xml to read this file in vb. this already works perfectly in a try catch statement but if another person has the file open then I get an exception message

    what I would like to do is if filename is locked then open a cached version instead

    could I in the catch stamens open the cached version but I would need to check if the error was defiantly file locked and not a different reason
    any ideas?

    many thanks Tim

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: file locked

    Use a Try Catch....with two Catches ... the first being for the specific exception of a locked file, the second being for the generic exception ... handle the opening of the cached file in the first exception.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2020
    Posts
    29

    Re: file locked

    ok I guess I could compare the string it returns with what I expect it to be.

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: file locked

    Huh? What string?
    No... you CATCH the SPECIFIC exception:
    Code:
    Catch ex As IOException
      ' Replace this with your cached file here
        Console.WriteLine( _
            "{0}: The write operation could " & _
            "not be performed because the " & _
            "specified part of the file is " & _
            "locked.", ex.GetType().Name)
    Catch ex as Exception
       ' Put your generic exception handler here
    End Try
    Yes... you can do that... in fact, it's the way you should be doing exception handling.... catch specific exceptions first, then moving towards more generic ones as you move down.

    https://docs.microsoft.com/en-us/dot...ew=netcore-3.1


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Aug 2020
    Posts
    29

    Re: file locked

    Thanks
    That works a treat

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