【问题标题】:Is writing "System.AppContext" in code behind not supported in .net 4.5.1?.net 4.5.1 不支持在后面的代码中编写“System.AppContext”吗?
【发布时间】:2021-05-26 02:29:24
【问题描述】:

我的.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 不支持“代码隐藏设置”吗?

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-28
      • 2014-02-09
      • 1970-01-01
      • 2013-03-16
      • 1970-01-01
      相关资源
      最近更新 更多