Setting Switch in Code

Viewed 1124

I'm trying to embed the following switch setting in my console application but I need the app.config to not be necessary. Is there another way to set this switch within the app?

I have come across AppContext.SetSwitch but this is only available in .NET 4.6, but my app will need to run on XP machines. Is there another way of doing this?

<runtime>
  <AppContextSwitchOverrides value="Switch.System.IO.UseLegacyPathHandling=false;Switch.System.IO.BlockLongPaths=false" />
</runtime>
2 Answers
var type = Type.GetType("System.AppContext");
if (type != null) {
    var 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 });
}

I have used this code in my application , but the value inside the setSwitch to which should i compare for long path

Related