Is writing "System.AppContext" in code behind not supported in .net 4.5.1?

Viewed 278

I need long path support for my .net 4.5.1 WPF application.

This setting in App.config works (long paths are supported):

<runtime>
    <AppContextSwitchOverrides value="Switch.System.IO.UseLegacyPathHandling=false;Switch.System.IO.BlockLongPaths=false" />
</runtime>

So I tried to set in in code, but it does not change the behaviour:

public App()
{
    // returns correct type:
    Type type = Type.GetType("System.AppContext"); 
    if (type != null)
    {
        // returns correct switch:
        MethodInfo setSwitch = type.GetMethod("SetSwitch", BindingFlags.Public | BindingFlags.Static);
        setSwitch.Invoke(null, new object[] { "Switch.System.IO.UseLegacyPathHandling", false });
        setSwitch.Invoke(null, new object[] { "Switch.System.IO.BlockLongPaths", false });
    }
}

Is that "code behind setting" not supported in .net 4.5.1?

1 Answers

Is that "code behind setting" not supported in .net 4.5.1?

No, it isn't. You should use AppContext.SetSwitch to set the switches programmatically but since this API was introduced in .NET Framework 4.6, the only officially supported way to set the switches on earlier versions is to use the App.config file.

Related