-
Sep 15th, 2020, 07:08 AM
#1
Thread Starter
Junior Member
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
-
Sep 15th, 2020, 07:10 AM
#2
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
-
Sep 15th, 2020, 07:25 AM
#3
Thread Starter
Junior Member
Re: file locked
ok I guess I could compare the string it returns with what I expect it to be.
-
Sep 15th, 2020, 07:32 AM
#4
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
-
Sep 20th, 2020, 07:55 AM
#5
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|