简体   繁体   中英

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

value from database: dbpath : LearnerAnswers\\test.pkg

Expected output : d:\\Storage\\ResourceStorage\\LearnerAnswers\\test.pkg

Actual output : D:\\LearnerAnswers\\test.pkg

Updated question to reflect exact scenario

value from debugger for store path : 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 "\\\\" ?

Path.Combine assumes you want root directory if your second variable starts with "\\\\" or @"\\"

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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