Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Path.Combine and the dot notation [duplicate]

I'm looking for something akin to Path.Combine method that will help me correctly combine absolute and relative paths. For example, I want

Path.Combine(@"c:\alpha\beta", @"..\gamma"); 

to yield c:\alpha\gamma instead of c:\alpha\..\gamma as Path.Combine does. Is there any easy way of accomplishing this? Needless to say, I also want to period . path or multiple .. paths (e.g., ..\..\) to work correctly.

like image 371
Dmitri Nesteruk Avatar asked Jan 27 '10 18:01

Dmitri Nesteruk


People also ask

What does double dot mean in path?

A relative path refers to a location that is relative to a current directory. Relative paths make use of two special symbols, a dot (.) and a double-dot (..), which translate into the current directory and the parent directory. Double dots are used for moving up in the hierarchy.

What does dot mean in file path?

(dot dot) This refers to the parent directory of your working directory, immediately above your working directory in the file system structure. If one of these is used as the first element in a relative path name, it refers to your working directory.

What does path combine do?

Combines strings into a path.

What is the system io path?

Remarks. A path is a string that provides the location of a file or directory. A path does not necessarily point to a location on disk; for example, a path might map to a location in memory or on a device. The exact format of a path is determined by the current platform.


1 Answers

Use Path.GetFullPath

string path = Path.Combine(@"c:\alpha\beta", @"..\gamma"); Console.WriteLine(Path.GetFullPath(path)); 

or the DirectoryInfo class:

string path = Path.Combine(@"c:\alpha\beta", @"..\gamma"); DirectoryInfo info = new DirectoryInfo(path); Console.WriteLine(info.FullName); 

Both will output:

c:\alpha\gamma 
like image 137
jason Avatar answered Sep 23 '22 11:09

jason