Path.Combine和点符号 [英] Path.Combine and the dot notation

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

问题描述

我正在寻找类似于 Path.Combine 的方法,这将有助于我正确地组合绝对路径和相对路径。例如,我想要

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");

以产生 c:\alpha\gamma 而不是 c:\alpha\..\gamma 作为 Path.Combine 。有没有办法完成这个?不用说,我也想要期限路径或多个 .. 路径(例如, .. \..\ )正常工作。

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.

推荐答案

使用 Path.GetFullPath

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

DirectoryInfo 类:

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

都会输出:

c:\alpha\gamma

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

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