Reputation: 1616
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>
Upvotes: 3
Views: 1595
Reputation: 1
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
Upvotes: -2
Reputation: 101493
If you target version lower than .NET 4.6 and want to do that without app.config, you can do this:
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 });
}
That way, if your application currently runs on .NET 4.6+ (where AppContext
is available and where those switches will actually have any effect) - you set them, otherwise do nothing.
Upvotes: 6