C# 如果给定短路径,应用程序无法从应用程序配置读取

C# 如果给定短路径,应用程序无法从应用程序配置读取,c#,vb.net,app-config,C#,Vb.net,App Config,如果我指定文件的短路径并运行应用程序,则它将无法从exe.config文件获取设置。 例如,我在app.config中给出了以下设置 <setting name="test" serializeAs="String"> <value>abc</value> </setting> 它按预期显示abc 如果我将设置更改为其他值,例如 <setting name="test" serializeAs="String

如果我指定文件的短路径并运行应用程序,则它将无法从exe.config文件获取设置。 例如,我在app.config中给出了以下设置

<setting name="test" serializeAs="String">
                <value>abc</value>
</setting>
它按预期显示
abc

如果我将设置更改为其他值,例如

<setting name="test" serializeAs="String">
                    <value>xyz</value>
</setting>

xyz
然后从调试文件夹中运行应用程序(D:\Desktop\test\bin\Debug\test test 123.exe),然后它会显示我更改过的
xyz

但是,如果我指定文件的短路径(D:\Desktop\test\bin\Debug\TESTTE~2.EXE)并运行(可以从windows运行中运行),那么它将显示
abc
本身而不是xyz

那个么,当给定短路径时,为什么应用程序无法读取应用程序配置文件呢

编辑:您可以下载代码
要查看错误,您可以使用长路径和短路径运行\test\bin\Release\test test 123.exe文件(我已将配置更改为xyz以设置测试)

我不确定它失败的原因,但如果您的代码中需要8.3版本的路径,我建议将完整路径放在配置文件中,然后使用Windows API获取8.3路径。这似乎没有本机的.NET函数。但是,我在CodeBlog上发现了一些片段

相关片段:

public static String GetShortPathName(String longPath)
{
    StringBuilder shortPath = new StringBuilder(longPath.Length + 1);

    if (0 == PathExtensions.GetShortPathName(longPath, shortPath, shortPath.Capacity))
    {
        return longPath;
    }

    return shortPath.ToString();
}

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern Int32 GetShortPathName(String path, StringBuilder shortPath, Int32 shortPathLength);

是My.Settings.test应用程序还是用户设置?您是否尝试在项目设置中进行同步?@DavidSdot其in app.config所有设置都在app.config中,但存在应用程序设置和用户设置,您的设置在哪个分支中,或者您能否显示整个app.config文件?
public static String GetShortPathName(String longPath)
{
    StringBuilder shortPath = new StringBuilder(longPath.Length + 1);

    if (0 == PathExtensions.GetShortPathName(longPath, shortPath, shortPath.Capacity))
    {
        return longPath;
    }

    return shortPath.ToString();
}

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern Int32 GetShortPathName(String path, StringBuilder shortPath, Int32 shortPathLength);