Path.Combine()未按预期工作

时间:2016-05-27 08:41:27

标签: c# path system.io.file

 var storePath = ConfigurationManager.AppSettings[configKey]; 
 var dbpath=dbpath.replace("/","\\")
 var fullFilePath = Path.Combine(storePath, dbpath);

存储在配置密钥中的值 - > d:\Storage\ResourceStorage

来自数据库的

值: dbpath:LearnerAnswers\test.pkg

预期输出: d:\Storage\ResourceStorage\LearnerAnswers\test.pkg

实际输出: D:\LearnerAnswers\test.pkg

更新了问题以反映具体情况

来自商店路径调试器的

值: d:\Storage\ResourceStorage

我花了很多时间在这上面......但是找不到什么问题?

2 个答案:

答案 0 :(得分:0)

我已经检查了你在问题中提供的示例路径,我得到了完全预期的输出。

var storePath = @"d:\Storage\ResourceStorage"; 
var dbpath = @"LearnerAnswers\test.pkg"; 
var fullFilePath = Path.Combine(storePath, dbpath);

必须有别的东西是错的。请在单步模式下使用调试器并验证每个值。

以下原始答案是由于问题中首先提供的信息无效。

您需要在此处引用反斜杠或使用@

var storePath = "d:\Storage\ResourceStorage";

请使用以下其中一项:

var storePath = @"d:\Storage\ResourceStorage";
var storePath = "d:\\Storage\\ResourceStorage";

<击>

答案 1 :(得分:0)

DBPath是否以"\\"开头?

Path.Combine假设您的第二个变量以"\\"@"\"

开头,则需要根目录

Path.Combine("C:\\test", "\\NewFolder")返回"c:\\NewFolder"

Path.Combine("C:\\test", "NewFolder")返回"c:\\test\\NewFolder"