none
How to get "maybeFullPath" property from a System.IO.IOException RRS feed

  • Question

  • Hi all,

    I am trying to do a simple thing, but unable to do it...

    I wrote an exception information provider that can provide informations when an exception is thrown. One of them is an information about what process locks a file if a System.IO.IOException is trhrown because of a file lock (0x80070020 error code).

    At the Exception provider level, I have no way to know what is the file that is being opened. I need at least to know the path of the file in order to detect what process locks it.

    When debugging, I saw that there is a property within the Exception called "_maybeFullPath". That property contains the path of the file. But it's a private property, and it's a property of a parent object of System.IO.IOException.

    I would like to get that property. I tried the following code, but it returns null :

    string path = (string) t.GetProperty("_maybeFullPath").GetValue(myIOException, null);

    Can you help me ?

    Thank you!

    Best regards.


    Matteo, .NET Developer and System Engineer

    Thursday, March 14, 2013 1:46 PM

Answers

  • It's a field, not a property so you need to use GetField.

    var field = type.GetField("_maybeFullPath", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCae);

    Also note that I did a quick search and this field is set in only 3 places, none of which are probably code you'd call so I doubt you'll ever get anything meaningful out of this field.

    • Marked as answer by joujoukinder Thursday, March 14, 2013 3:04 PM
    Thursday, March 14, 2013 2:50 PM
    Moderator

All replies

  • I cannot possibly recommend that you do that.  An IOException doesn't necessarily mean there is a file involved.  Making that assumption is dangerous. 

    Private/internal members can change at any time (even between SPs of the framework).  If you write code to rely on it then you're setting yourself up for an eventual failure that you're going to have to debug.  Furthermore getting such members using reflection requires full trust which can limit the useful of your code.  I would recommend that you stick with the public properties and the Data attribute of the exception. 

    If you really want to do it (again not recommended at all):

    var prop = exceptionType.GetProperty("_maybeFullPath", BindingFlags.NonPublic);
    var value = (prop != null) ? prop.GetValue(object, null) : null;
    var path = (value != null) ? value.ToString() : "";

    Michael Taylor - 3/14/2013
    http://msmvps.com/blogs/p3net

    Thursday, March 14, 2013 2:11 PM
    Moderator
  • Hi Michael,

    Thank you for your response.

    I know it's not a good idea, but I will try to change it later.

    I tried your code, but the GetProperty(...) method returns null :

    System.Type t = ex.GetType();
    PropertyInfo pi = t.GetProperty("_maybeFullPath", System.Reflection.BindingFlags.NonPublic) ; // returns NULL

    But looking at the watch in Visual Studio, the property exists...

    What do I do wrong ?

    Thanks.

    Regards.


    Matteo, .NET Developer and System Engineer

    Thursday, March 14, 2013 2:33 PM
  • It's a field, not a property so you need to use GetField.

    var field = type.GetField("_maybeFullPath", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCae);

    Also note that I did a quick search and this field is set in only 3 places, none of which are probably code you'd call so I doubt you'll ever get anything meaningful out of this field.

    • Marked as answer by joujoukinder Thursday, March 14, 2013 3:04 PM
    Thursday, March 14, 2013 2:50 PM
    Moderator
  • Thank you! It works.

    Sorry about the field/property, I am not used to reflection!

    Best regards.


    Matteo, .NET Developer and System Engineer

    Thursday, March 14, 2013 3:03 PM