Path.Combine和点符号 [英] Path.Combine and the dot notation
问题描述
我在找一个类似于 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$c$c>
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$c$c>类:
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屋!