Partha
Partha

Reputation: 2192

problem in using path.combine statement in c#

    string targetPath = @"C:\Program Files\saadhvi\SetupSafetyPADUniversal\";
    string createDatabasesScriptFilePath = Path.Combine(targetPath, "\\EADBScripts\\CreateDatabases.sql");

i am getting the value of createDatabasesScriptFilePath is \EADBScripts\CreateDatabases.sql

but i expected it would be C:\Program Files\saadhvi\SetupSafetyPADUniversal\EADBScripts\CreateDatabases.sql

what is the wrong with my code?

Upvotes: 9

Views: 2649

Answers (4)

XXXXX
XXXXX

Reputation: 1096

Remove the initial backslash from "\EADBScripts..." in the second argument.

Upvotes: 2

malay
malay

Reputation: 1472

Here is why your code is returning the 2nd path (copied from MSDN help) -

If path2 does not include a root (for example, if path2 does not start with a separator character or a drive specification), the result is a concatenation of the two paths, with an intervening separator character. If path2 includes a root, path2 is returned.

Upvotes: 16

KV Prajapati
KV Prajapati

Reputation: 94653

string targetPath = @"C:\Program Files\saadhvi\SetupSafetyPADUniversal\"; 
string createDatabasesScriptFilePath;
createDatabasesScriptFilePath= Path.Combine(targetPath, "EADBScripts\\CreateDatabases.sql");

Upvotes: 1

StampedeXV
StampedeXV

Reputation: 2815

Remove the first \ from the string "\EADBScripts\CreateDatabases.sql"

I'm not completely sure of the reason, but a I guess Path.Combine wants as second parameter a relative path, and a relative Path does not start with a \.

Upvotes: 11

Related Questions