.Net 4.6.2 C#长路径处理-仅在发布版本中引发Ilegal字符异常

.Net 4.6.2 C#长路径处理-仅在发布版本中引发Ilegal字符异常,c#,visual-studio-2013,.net-4.6.2,long-filenames,C#,Visual Studio 2013,.net 4.6.2,Long Filenames,我将我的项目切换到.NET4.6.2以避免最大路径问题。我还读到,前缀“\\?\”允许更长的路径。我这样做了,但现在有一个奇怪的问题。使用调试配置生成时,它正在工作,而发布配置生成正在抛出“路径中的非法字符”。执行此操作时出现异常,例如: DirectoryInfo di = new DirectoryInfo(ConfigManager.PluginPath); public string AppPathRoot { get { // INFO: using long

我将我的项目切换到.NET4.6.2以避免最大路径问题。我还读到,前缀“\\?\”允许更长的路径。我这样做了,但现在有一个奇怪的问题。使用调试配置生成时,它正在工作,而发布配置生成正在抛出“路径中的非法字符”。执行此操作时出现异常,例如:

DirectoryInfo di = new DirectoryInfo(ConfigManager.PluginPath);
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);
    }
}
根据调试器,这两种情况下的组合路径都相同

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);
    }
}
我目前不知道出了什么问题,因为所有项目都切换到了4.6.2,而且这不取决于配置。还是我错过了一个选择

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);
    }
}
编辑: 同时,我尝试了一个非常简单的环境,使用控制台应用程序来确保没有副作用。有趣的是,它现在在这两种情况下都抛出,所以发布/调试之间没有区别。路径已存在。我用Windows7上的VS2013构建这个

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

    }
}
编辑2:
看起来VS2013的行为与VS2017不同。VS2017不会抛出异常,VS2013会…

完整路径是什么?您是否尝试过path.Combine()而不是string.format()?@Trey:问题是,在这两种情况下,路径都是完全相同的,开始时是这样的:\\?\C:\ u Dev\。。。唯一的区别是调试或发布版本。这可能是一个配置问题,但我不知道我们该怎么看。Targetframework是一个全局项目设置,因此在这两种情况下都是4.6。2@ZohirSalakCeNa:这可能不会改变任何东西,因为在调试器中查看路径时路径是相同的。但是只有发布版失败了。我在发布版和调试版上都尝试过添加“\\\?\{0}”和不添加“\\\\?\{0}”,效果似乎很好。我还尝试在windows中超过可能的路径长度,但效果很好
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);
    }
}