Sai Avinash
Sai Avinash

Reputation: 4753

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 ?

Upvotes: 1

Views: 3005

Answers (2)

Mark Hebberd
Mark Hebberd

Reputation: 61

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"

Upvotes: 4

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56727

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";

Upvotes: 0

Related Questions