IO locked error

B

Brian Henry

When a user has a file open and of course .NET cant access it due to it
being locked i get an error back like this through an exception

System.IO.IOException: The process cannot access the file 'S:\Care.xls'
because it is being used by another process.

is there any way to trap that specific error and give a user friendly
message back? but on any other errors throw an exception? thanks!
 
A

Armin Zingler

Brian Henry said:
When a user has a file open and of course .NET cant access it due to
it being locked i get an error back like this through an exception

System.IO.IOException: The process cannot access the file
'S:\Care.xls' because it is being used by another process.

is there any way to trap that specific error and give a user
friendly message back? but on any other errors throw an exception?
thanks!


try
'code
catch ex as System.IO.IOException
'user friendly message
end try


Armin
 
B

Brian Henry

That traps all IO errors though... I just want something when the file is
locked to say something like "You already have this file open, please close
it and try agian" before you could do this with error codes, with exceptions
you dont seem to have them anymore that I know of

I still want IO errors like file not found and stuff to throw exceptions
outside of the one i already mentioned
 
G

Guest

Brian,

You could examine the exception's Message property. But that runs the risk
that the message could change in future versions of the framework.

Kerry Moorman
 
A

Armin Zingler

Brian Henry said:
That traps all IO errors though... I just want something when the
file is locked to say something like "You already have this file
open, please close it and try agian" before you could do this with
error codes, with exceptions you dont seem to have them anymore that
I know of

I still want IO errors like file not found and stuff to throw
exceptions outside of the one i already mentioned

You wrote that you want to catch only IOException, not all other exceptions.
That's what the example does.

You could rethrow the exception if it's one of the exceptions derived from
IOException: (untested)

try
'code
catch ex as ioexception
if ex.gettype is gettype(ioexception) then
'message here
else
throw
end if
end try


Another way:
const ERROR_SHARING_VIOLATION as integer = 32

try
'code
Catch ex As IOException When
System.Runtime.InteropServices.Marshal.GetLastWin32Error =
ERROR_SHARING_VIOLATION
'message
end try


However, I'm not sure whether error #32 is the only error that indicates a
sharing violation.


Armin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top