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.
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.
(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.
Combines strings into a 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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With