1
 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 ?

Sai Avinash
  • 4,683
  • 17
  • 58
  • 96
  • Something wrong in your example above? Are you using _dbpath_ or _path_? – Steve May 27 '16 at 08:46
  • @Steve..dbpath is just my variable name – Sai Avinash May 27 '16 at 08:47
  • Your dbPath is not used in the example above, so why did you include it at all? – Evk May 27 '16 at 08:48
  • Corrected question now – Sai Avinash May 27 '16 at 08:49
  • Did not become any better, dbPath is still not used in Path.Combine :) – Evk May 27 '16 at 08:49
  • Also I assume that your _storePath_ is assigned with @ before or contains double backslashes otherwise you are not able to compile that code _unrecognized escape sequence_ on `\S` – Steve May 27 '16 at 08:53
  • There is no way that the correct code could produce that output. You need to use the debugger to understand what happens here. – Steve May 27 '16 at 08:55
  • 2
    Why is everone using `ConfigurationManager.AppSettings` with the possibility of misspelling the key instead of the conveniently created property in `Properties.Settings.Default`!? – Thorsten Dittmar May 27 '16 at 08:56
  • Have you checked the value of storePath using debugger – Ranjit Singh May 27 '16 at 08:57
  • Updated value from debugger – Sai Avinash May 27 '16 at 08:58
  • 1
    This code is working as expected, so you are getting wrong input somewhere. var storePath = @"d:\Storage\ResourceStorage"; var dbpath = @"LearnerAnswers\test.pkg"; var fullFilePath = Path.Combine(storePath, dbpath); –  May 27 '16 at 09:21
  • 1
    See [this answer](https://stackoverflow.com/a/53118/7794769) for how [Path.Combine](https://learn.microsoft.com/en-us/dotnet/api/system.io.path.combine?view=netcore-3.1#remarks) works under the hood. It explains the unexpected behavior. – stomy Jul 13 '20 at 20:05

2 Answers2

4

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"

d219
  • 2,707
  • 5
  • 31
  • 36
0

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

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • 1
    Does that even compiles otherwise? – Evk May 27 '16 at 08:53
  • 1
    Now that you say it: it shouldn't, but as the OP says in another comment, this is not his real code... – Thorsten Dittmar May 27 '16 at 08:53
  • Not from me, so no idea. Maybe because your answer does not help, because it won't compile otherwise anyway (and it compiles for OP). Of course it's just bad question and not your fault. – Evk May 27 '16 at 09:03
  • @ThorstenDittmar. I did not downvote as i did not frame the question properly – Sai Avinash May 27 '16 at 09:05
  • The original question was full of errors but now after the fixing both answers are meaningless. Better delete them because new readers cannot understand immediately now what has generated your answers. – Steve May 27 '16 at 09:23