Path.Combine 绝对路径和相对路径字符串 [英] Path.Combine absolute with relative path strings

查看:54
本文介绍了Path.Combine 绝对路径和相对路径字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Path.Combine.

I'm trying to join a Windows path with a relative path using Path.Combine.

然而,Path.Combine(@"C:lah",@"..ling") 反而返回 C:lah..lingC:ling.

有谁知道如何在不编写自己的相对路径解析器的情况下完成此操作(这应该不会太难)?

Does anyone know how to accomplish this without writing my own relative path resolver (which shouldn't be too hard)?

推荐答案

什么有效:

string relativePath = "..\bling.txt";
string baseDirectory = "C:\blah\";
string absolutePath = Path.GetFullPath(baseDirectory + relativePath);

(结果:absolutePath="C:ling.txt")

(result: absolutePath="C:ling.txt")

什么不起作用

string relativePath = "..\bling.txt";
Uri baseAbsoluteUri = new Uri("C:\blah\");
string absolutePath = new Uri(baseAbsoluteUri, relativePath).AbsolutePath;

(结果:absolutePath="C:/blah/bling.txt")

(result: absolutePath="C:/blah/bling.txt")

这篇关于Path.Combine 绝对路径和相对路径字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆