如何建立相对于特定文件夹的绝对路径? [英] How to make an absolute path relative to a particular folder?

查看:102
本文介绍了如何建立相对于特定文件夹的绝对路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我该怎么做

"C:\RootFolder\SubFolder\MoreSubFolder\LastFolder\SomeFile.txt"

相对于此文件夹

"C:\RootFolder\SubFolder\"

如果预期结果是

"MoreSubFolder\LastFolder\SomeFile.txt"

推荐答案

是的,您可以轻松做到,将路径视为URI :

Yes, you can do that, it's easy, think of your paths as URIs:

Uri fullPath = new Uri(@"C:\RootFolder\SubFolder\MoreSubFolder\LastFolder\SomeFile.txt", UriKind.Absolute);
Uri relRoot = new Uri(@"C:\RootFolder\SubFolder\", UriKind.Absolute);

string relPath = relRoot.MakeRelativeUri(fullPath).ToString();
// relPath == @"MoreSubFolder\LastFolder\SomeFile.txt"

这篇关于如何建立相对于特定文件夹的绝对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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