如何从基本路径和包含“..”的相对路径获取绝对文件路径?
string basepath = @"C:\somefolder\subfolder\bin"; // is defined in runtime
string relative = @"..\..\templates";
string absolute = Magic(basepath, relative); // should be "C:\somefolder\templates"
你能帮我使用Magic
方法吗?希望代码不要太复杂。
.NET Framework 中有“Magic
”方法吗?
string basepath = @"C:\somefolder\subfolder\bin"; // is defined in runtime
string relative = @"..\..\templates";
string absolute = Magic(basepath, relative); // should be "C:\somefolder\templates"
Can you help me with Magic
method? Hopefully not too complicated code.
Is there the "Magic
" method in .NET Framework?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您查看
路径
类有几个方法应该有帮助:所以
:
虽然第二步并不是严格需要的 - 如果您打印出来的话,它会给您一个“更干净”的路径。
If you look at the
Path
class there are a couple of methods which should help:and
So:
Although the second step isn't strictly needed - it would give you a "cleaner" path if you were printing out say.
因为
Path.Combine
并非在所有情况下都有效,这里有一个更复杂的函数:-)Path.Combine(@"C:\foo\",@"\foo\bar")
返回@"\foo\bar"
而不是预期的@"C:\foo\bar"
Because
Path.Combine
does not work in all cases here is a more complex function :-)Path.Combine(@"C:\foo\",@"\foo\bar")
returns@"\foo\bar"
and not as expected@"C:\foo\bar"