我正在开发的应用程序需要处理文件/路径名非常长的文件。它是一个.Net 4.6应用程序,所以我已经实现了4.6.2之前的解决方案,允许使用“\?”?\语法概述
here
和
here
.
这是我用于启用该功能的代码(我无法修改app.config,因此必须在代码中设置):
var type = Type.GetType("System.AppContext");
if (type != null)
{
AppContext.SetSwitch("Switch.System.IO.UseLegacyPathHandling", false);
AppContext.SetSwitch("Switch.System.IO.BlockLongPaths", false);
var switchType = Type.GetType("System.AppContextSwitches");
if (switchType != null)
{
// We also have to reach into System.AppContextSwitches and manually update the cached private versions of these properties (don't ask me why):
var legacyField = switchType.GetField("_useLegacyPathHandling", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
legacyField?.SetValue(null, (Int32)(-1)); // <- caching uses 0 to indicate no value, -1 for false, 1 for true.
var blockingField = switchType.GetField("_blockLongPaths", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
blockingField?.SetValue(null, (Int32)(-1)); // <- caching uses 0 to indicate no value, -1 for false, 1 for true.
}
}
这个有用(耶!)在我们测试过的所有机器上,
除了一个
(嘘!)。所讨论的计算机与其他计算机一样是Windows 10 Pro安装,并且在[Computer\HKEY\u LOCAL\machine\SYSTEM\CurrentControlSet\Control\FileSystem]命名空间中具有相同的注册表设置。
此特定计算机上的错误消息是:
不支持给定的路径格式
我们在这台机器上看到的一个区别是,当在Windows文件资源管理器中查看一个很长的文件时,“Location”字段使用“\?”?\r-click>属性菜单中的语法。
我猜是某个注册表项导致了文件资源管理器中的差异和我的修复失败,但不是上面提到的文件系统命名空间。
有没有人遇到过类似的问题,或者对其他可能相关的注册领域有了想法?