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

查看:134
本文介绍了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:\阿尔法\伽玛而不是 C:\阿尔法\ .. \伽玛 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.

推荐答案

使用<一个href="http://msdn.microsoft.com/en-us/library/system.io.path.getfullpath.aspx"><$c$c>Path.GetFullPath

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

或<一href="http://msdn.microsoft.com/en-us/library/system.io.directoryinfo.aspx"><$c$c>DirectoryInfo类:

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

双方将输出:

Both will output:

c:\alpha\gamma

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

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