简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM