Visual Basic .Net, Need the Specific reason an IOException was thrown.

MikeBroderick
MikeBroderick used Ask the Experts™
on
Is there an equivilant to C's errno in catching an IOException in .Net? I have an app that tests for locks, and I want to make sure I am waiting because the error was a locking problem, not another problem ("Access Denied", etc.). I do not want to parse the error string (could change, not language independent) or retrieve process list and look for my file (too much CPU). Is there a feature of .Net that works like errno?

Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2015
Commented:
Possibly?

Marshal.GetLastWin32Error, or Err.LastDllError
Shahan AyyubSenior Software Engineer

Commented:
Does this table provide you help:


Error Number  
Default Error Message  
5  
Procedure call or argument is not valid  
6  
Overflow  
7  
Out of Memory  
11  
Division by Zero  
51  
Internal Error  
52  
Bad file name or number  
53  
File not found  
55  
File already open  
76  
Path not found  
482  
Printer error

Found from here:
http://www.codeproject.com/KB/dotnet/VB_NET_Error_Handling.aspx
Shahan AyyubSenior Software Engineer

Commented:
Try this one:

        Try
            Throw New IOException()
        Catch ex As Exception
            Console.WriteLine(Err.Number)
        End Try
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

Author

Commented:
Perfect! Both work. I picked the Err obj. The file locked at open gives 32 and the filestream.Lock() gives  33. They are documented here:

http://msdn.microsoft.com/en-us/library/ms681382(v=VS.85).aspx

Thanks
Shahan AyyubSenior Software Engineer

Commented:
did my solution work for you ??

Author

Commented:
Sorry Shahan, I was testing TommyBoy's post and didn't see yours when I posted my answer. I did try yours though. I got a 57 back, which wasnt documented in the codeproject page. Thanks anyway.
Shahan AyyubSenior Software Engineer

Commented:
but this post was correct ID: 33130416
Shahan AyyubSenior Software Engineer

Commented:
>>>> Is there an  equivilant to C's errno

the post referring Err.Number was according to your question.
Shahan AyyubSenior Software Engineer

Commented:
but any ways you can use both. No problem!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial