首页 > 解决方案 > .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 会...

标签: c#visual-studio-2013.net-4.6.2long-filenames

解决方案


推荐阅读