c# - .net 4.5.1 不支持在代码后面写入 "System.AppContext"吗?

标签 c# wpf .net-4.5 app-config

我的 .net 4.5.1 WPF 应用程序需要长路径支持。

App.config 中的此设置有效(支持长路径):

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

所以我尝试在代码中设置,但它不会改变行为:

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 });
    }
}

.net 4.5.1 不支持“代码隐藏设置”吗?

最佳答案

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

不,不是。您应该使用AppContext.SetSwitch以编程方式设置开关,但由于此 API 是在 .NET Framework 4.6 中引入的,因此在早期版本上设置开关的唯一官方支持的方法是使用 App.config 文件。

关于c# - .net 4.5.1 不支持在代码后面写入 "System.AppContext"吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66339560/

相关文章:

C# 泛型和类型检查

c# - System.Threading 休眠功能有帮助吗? (C#)

c# - 从 Ilist 获取 Idictionnary

c# - 如何在通用文本 block 中显示所有验证错误消息

c# - WPF: MemoryStream 占用大量内存

.net - TargetType ="controlType"和 TargetType ="{x:Type controlType}"之间的区别

c# - Graphics MeasureString 方法是否/可以考虑容器的宽度和换行?

c# - C#中的文件创建时间

c# - 如何以编程方式检索 "Program Files"文件夹的实际路径?

c# - 在 TeamCity 中包含 NuGet 包