如何从文件路径中删除版本号? - Winforms c#

use*_*802 2 c# filepath winforms

我想知道如何从Windows窗体应用程序中的文件路径中删除版本号.

目前,我希望将一些用户应用程序数据保存到位于漫游用户配置文件设置中的.xml文件中.

为此,我使用:

get
{
     return Application.UserAppDataPath + "\\FileName.xml";
}
Run Code Online (Sandbox Code Playgroud)

但是,这将返回以下字符串:

C:\Users\user\AppData\Roaming\folder\subfolder\1.0.0.0\FileName.xml

我想知道是否有一种非黑客方法从文件路径中删除版本号,所以文件路径如下所示:

C:\Users\user\AppData\Roaming\folder\subfolder\FileName.xml

除了解析字符串寻找最后一个"\",我不知道该怎么做.

谢谢

Sri*_*vel 8

为此目的使用Directory.GetParent方法.

get
{
    var dir = Directory.GetParent(Application.UserAppDataPath);
    return Path.Combine(dir.FullName, "FileName.xml");
}
Run Code Online (Sandbox Code Playgroud)

另请注意,我使用了Path.Combine而不是连接路径,这种方法可以帮助您避免这么多问题.永远不要连接字符串来创建路径.