As an example, assume the following snippet of VB.NET code to delete a directory.
Try
Dim SomeFolder="c:\somefolder"
System.IO.Directory.Delete(SomeFolder, True)
Catch ioex As System.IO.IOException
'What went wrong?
'File locked by another process?
'File not found?
'something else?
End Try
In the exception handler, if the directory or a file inside it is open I'd like to give the user the opportunity to close the file and retry the operation, but only if the IOException was caused by a locking problem.
The problem is that the IOException can be thrown for any number of reasons, for example an invalid path or read-only flag set on the file. Each of these conditions sets a different value in the .message attribute of the exception object, but it just feels so wrong to hard-code a check for a specific string in the error message to detect the specific cause of the failure. I don't have a lot of confidence that the error strings will be consistently worded with future versions of .net and would hate to have to fuss about with writing localization code to deal with the possibility that the message is returned in something other than English.
There has got to be a better way to deal with what has to be an extremely common exception handling concern. Am I missing something?
Update/Clarification: Thanks for the answers so far, but I may have made my example a little too generic. For right now at least, I am specifically looking for a way to detect the condition of the file being locked by another process within the exception handler.