6

I have, essentially, the same problem as this poster, but in C#: Waiting until a file is available for reading with Win32

More information: we have code that calls File.Open in one of our projects, that occasionally dies when the file is already opened by another process (EDIT: or thread):

FileStream stream = File.Open(m_fileName, m_mode, m_access);
/* do stream-type-stuff */
stream.Close();

File.Open will throw an IOException (which is currently quietly swallowed somewhere), whose HResult property is 0x80070020 (ERROR_SHARING_VIOLATION). What I would like to do is this:

FileStream stream = null;
while (stream == null) {
    try {
        stream = File.Open(m_fileName, m_mode, m_access, FileShare.Read);
    } catch (IOException e) {
        const int ERROR_SHARING_VIOLATION = int(0x80070020);
        if (e.HResult != ERROR_SHARING_VIOLATION)
            throw;
        else
            Thread.Sleep(1000);
    }
}
/* do stream-type-stuff */
stream.Close();

However, HResult is a protected member of Exception, and cannot be accessed -- the code does not compile. Is there another way of accessing the HResult, or perhaps, another part of .NET I might use to do what I want?

Oh, one final caveat, and it's a doozy: I'm limited to using Visual Studio 2005 and .NET 2.0.

Community
  • 1
  • 1
Blair Holloway
  • 15,969
  • 2
  • 29
  • 28
  • Note that the `HResult` property is [no longer `protected` as of .NET Framework v4.5](https://learn.microsoft.com/dotnet/api/system.exception.hresult#remarks): "Starting with the .NET Framework 4.5, the HResult property's setter is protected, whereas its getter is public. In previous versions of the .NET Framework, both getter and setter are protected." – Lance U. Matthews May 04 '18 at 01:11

2 Answers2

8

You can call Marshal.GetHRForException() within the catch clause to get the error code. No need for reflection:

using System.Runtime.InteropServices;

if (Marshal.GetHRForException(e) == ERROR_SHARING_VIOLATION)
    ....
Igal Tabachnik
  • 31,174
  • 15
  • 92
  • 157
0

Your best bet is using reflection unfortunately. Of course, since you sleep for 1sec between attempts, the performance costs will most likely go unnoticed.

Blindy
  • 65,249
  • 10
  • 91
  • 131