radbyx
radbyx

Reputation: 9670

How to change an absolute path to a relative path in C# Windows Forms

My absolute path works for me :

const string fileName = 
@"C:\Udvikling\MapEditor\MapEditorProject\Data\Track1.xml";

But I want a relative path. I try'ed :

const string fileName = @"~\Data\Track1.xml";

I get this Error:

"Could not find a part of the path C:\Udvikling\RollerApp\RollerGame\MapEditorProject\bin\Debug\~\Data\Track1.xml'"

Upvotes: 0

Views: 2214

Answers (1)

Tigran
Tigran

Reputation: 62276

Do not add ~ character and use Path.Combine method to combine 2 paths.

More or less like this:

const string fileName = @"\Data\Track1.xml";

string combinedPath =  Path.Combine(@"C:\Udvikling\RollerApp\RollerGame\MapEditorProject\bin\Debug", 
         fileName);

Hope this helps.

Upvotes: 1

Related Questions