假设我有以下相对路径作为字符串:
"foo/./my/../bar/file.txt"
Run Code Online (Sandbox Code Playgroud)
有没有一种快速的方法来解析点(例如“..”和“.”),以便结果是:
"foo/bar/file.txt"
Run Code Online (Sandbox Code Playgroud)
我无法使用,Uri因为它不是绝对路径,而且我也无法使用Path.GetFullPath,因为这将添加执行应用程序的路径,以便我最终得到:
"C:\myAppPath\foo\bar\file.txt"
Run Code Online (Sandbox Code Playgroud)
(它也改变了“/”->“\”,但我并不特别介意这一点)
只是一个黑客,
string path = @"foo/./my/../bar/file.txt";
string newPath = Path.GetFullPath(path).Replace(Environment.CurrentDirectory, "");
Run Code Online (Sandbox Code Playgroud)
您可以使用Path.GetFullPath,它返回路径以及Environment.CurrentDirectory, use从解析的路径String.Replace中删除当前目录。
你最终会得到newPath = \foo\bar\file.txt
| 归档时间: |
|
| 查看次数: |
2097 次 |
| 最近记录: |