相关疑难解决方法(0)

为什么Path.Combine不在驱动器指示符后添加Path.DirectorySeparatorChar?

var actual = Path.Combine("c:", "filename");
var expected = @"c:\filename";
Assert.AreEqual(expected, actual);
Run Code Online (Sandbox Code Playgroud)

结果

{Assert.AreEqual failed. Expected:<c:\filename>. Actual:<c:filename>.
Run Code Online (Sandbox Code Playgroud)

为什么?

.net path path-combine

20
推荐指数
2
解决办法
2885
查看次数

Path.Combine()在驱动器号后面不添加目录分隔符

我使用的Path.Combine方法不正确吗?
我得到这个结果Path.Combine(string[]):

C:Users\\Admin\\AppData\\Roaming\\TestProject\\Connections.xml
Run Code Online (Sandbox Code Playgroud)

这是不太理想的代码的期望结果:由注释掉的代码生成的结果:

C:\\Users\\Admin\\AppData\\Roaming\\TestProject\\Connections.xml
Run Code Online (Sandbox Code Playgroud)

请注意\\第一个路径中的驱动器号后面缺少的.

旧的方法(手动添加反斜杠,注释掉)运行良好,但我很确定如果用mono编译,它在Linux或其他东西下不起作用.

string[] TempSave = Application.UserAppDataPath.Split(Path.DirectorySeparatorChar);
string[] DesiredPath = new string[TempSave.Length - 2];
for (int i = 0; i < TempSave.Length - 2; i++)
{
    //SaveLocation += TempSave[i] + "\\";//This Works
    DesiredPath[i] = TempSave[i];
}
SaveLocation = Path.Combine(DesiredPath);//This Doesn't Work.    
ConnectionsFs = new FileStream(Path.Combine(SaveLocation,FileName)/*SaveLocation+FileName*/, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
Run Code Online (Sandbox Code Playgroud)

基本上,我希望最终结果是TestProject目录,而不是项目本身或其版本.

.net c# path

10
推荐指数
1
解决办法
4981
查看次数

标签 统计

.net ×2

path ×2

c# ×1

path-combine ×1