How can one with minimal effort (using some already existing facility, if possible) convert paths like c:\aaa\bbb\..\ccc
to c:\aaa\ccc
?
To find the full absolute path of the current directory, use the pwd command. Once you've determined the path to the current directory, the absolute path to the file is the path plus the name of the file.
Normalizing a path involves modifying the string that identifies a path or file so that it conforms to a valid path on the target operating system. Normalization typically involves: Canonicalizing component and directory separators. Applying the current directory to a relative path.
Regardless of where you are working in the file system, you can always find a directory or file by specifying its absolute path name. Absolute path names start with a slash (/), the symbol representing the root directory. The path name /A/D/9 is the absolute path name for 9.
An absolute path is defined as specifying the location of a file or directory from the root directory(/). In other words,we can say that an absolute path is a complete path from start of actual file system from / directory. Relative path. Relative path is defined as the path related to the present working directly(pwd) ...
I would write it like this:
public static string NormalizePath(string path) { return Path.GetFullPath(new Uri(path).LocalPath) .TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar) .ToUpperInvariant(); }
This should handle few scenarios like
uri and potential escaped characters in it, like
file:///C:/Test%20Project.exe -> C:\TEST PROJECT.EXE
path segments specified by dots to denote current or parent directory
c:\aaa\bbb\..\ccc -> C:\AAA\CCC
tilde shortened (long) paths
C:\Progra~1\ -> C:\PROGRAM FILES
inconsistent directory delimiter character
C:/Documents\abc.txt -> C:\DOCUMENTS\ABC.TXT
Other than those, it can ignore case, trailing \
directory delimiter character etc.
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