简体   繁体   English

Path.Combine()无法正常工作

[英]Path.Combine() is not working as expected

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

Value stored in Config Key --> d:\\Storage\\ResourceStorage 存储在配置密钥-> d:\\Storage\\ResourceStorage

value from database: dbpath : LearnerAnswers\\test.pkg 数据库中的值: dbpath: LearnerAnswers\\test.pkg

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

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

Updated question to reflect exact scenario 更新了问题以反映确切的情况

value from debugger for store path : d:\\Storage\\ResourceStorage 调试器的存储路径值: d:\\Storage\\ResourceStorage

I have spent lot of time on this..But could not find out whats going wrong ? 我花了很多时间。.但是找不到什么地方出了问题吗?

I've checked with the example paths you gave in your question and I get exactly the expected output. 我已经检查了您在问题中给出的示例路径,并且得到了预期的输出。

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

There must be something else that's wrong. 肯定还有其他错误。 Please use the debugger in single step mode and verify every single value. 请在单步模式下使用调试器,并验证每个单个值。

The following original answer was due to the invalid information provided in the question at first. 以下原始答案是由于最初在问题中提供的信息无效。

You need to quote the backslashes here or use @ : 您需要在此处引用反斜杠或使用 @

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

So use one of the following: 因此,请使用以下之一:

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

Does DBPath start with a "\\\\" ? DBPath是否以"\\\\"开头?

Path.Combine assumes you want root directory if your second variable starts with "\\\\" or @"\\" 如果第二个变量以"\\\\"@"\\"开头,则Path.Combine假定您需要根目录

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM