Tomas
Tomas

Reputation: 18127

How to combine two path

I have the code below and I get the result like this

C:\\Users\\Administrator\\Projects\\CA\\Libraries\\ConvertApi-DotNet\\Example\\word2pdf-console\\bin\\Release\\\\..\\..\\..\\..\\test-files\\test.docx

The file is found but I would like to show user this path and the formating is not user friendly. I would like to get

C:\\Users\\Administrator\\Projects\\CA\\Libraries\\test-files\\test.docx 

I have tried to use Path.Combine but it do not work.

var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
string inFile = baseDirectory + @"\..\..\..\..\test-files\test.docx";

Upvotes: 1

Views: 227

Answers (2)

dknaack
dknaack

Reputation: 60556

Description

You say that the file is found.

Then you can use FileInfo (namespace System.IO) for that.

Sample

FileInfo f = new FileInfo(fileName);
f.Exists // Gets a value indicating whether a file exists.
f.DirectoryName // Gets a string representing the directory's full path.
f.FullName // Gets the full path of the directory or file.

More Information

Upvotes: 3

user7116
user7116

Reputation: 64148

You could use a combination of Path.Combine and Path.GetFullPath:

var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
var file = @"..\..\..\..\test-files\test.docx";
string inFile = Path.GetFullPath(Path.Combine(baseDirectory, file));

Upvotes: 3

Related Questions