c# - .Net 4.6.2 C# 长路径处理 - 仅在发布版本中引发非法字符异常
问题描述
我将我的项目切换到 .Net 4.6.2 以避免 MAX_PATH 问题。我还读过前面加上“\\?\”允许更长的路径。我这样做了,但现在有一个奇怪的问题。使用调试配置构建时,它正在工作,而发布配置构建抛出“路径中的非法字符”。这样做时的例外情况:
DirectoryInfo di = new DirectoryInfo(ConfigManager.PluginPath);
根据调试器,两者之间没有变化,并且在两种情况下组合路径都是相同的。
我目前不知道出了什么问题,因为所有项目都切换到 4.6.2 并且 afaik 这不依赖于配置。还是我在这里错过了一个选项?
public string AppPathRoot {
get {
// INFO: using long path prefix to prevent problems with MAX_PATH constraint
return string.Format("\\\\?\\{0}", Path.GetDirectoryName(Application.ExecutablePath));
//return Path.GetDirectoryName(Application.ExecutablePath);
}
}
编辑:我同时尝试了一个非常简单的环境,使用控制台应用程序来确保没有副作用。有趣的是,它现在在两种情况下都会抛出,所以发布/调试之间没有区别。路径是存在的。我在 Windows 7 上使用 VS2013 构建它。
class Program
{
static void Main(string[] args)
{
string sPathL = "\\\\?\\C:\\temp";
string sPath = "C:\\temp";
// doesn't throw
DirectoryInfo di = new DirectoryInfo(sPath);
// throws
DirectoryInfo di2 = new DirectoryInfo(sPathL);
}
}
Edit2:看起来 VS2013 的行为与 VS2017 不同。VS2017 不会抛出异常,VS2013 会...
解决方案
推荐阅读
- python - Python - 在不使用范围的情况下计算素数除数的数量
- excel - 将 TextBox 值插入列中的第一个空单元格
- c# - 如何将不同的数据库上下文从两个派生类传递给 C# 中的基类
- flutter - 如何将库添加到颤振依赖项
- android - 我可以在片段活动之外替换片段布局吗
- ios - 如何使用 reloadItems 淡化项目(在:[indexPath])
- amazon-web-services - aws 上的异步 lambda 函数是按顺序调用的吗?
- node.js - 保存 mongoose 数组模式类型时忽略 null 或空值
- php - PHP Heroku 应用程序不允许我访问子目录
- typescript - 在返回承诺的函数上使用 .then 会抛出“这是不可调用的”