如何根据一个绝对文件路径生成一个相对文件路径

您所在的位置:网站首页 文件路径生成器 如何根据一个绝对文件路径生成一个相对文件路径

如何根据一个绝对文件路径生成一个相对文件路径

2024-07-11 16:47| 来源: 网络整理| 查看: 265

日常的开发中,获取绝对文件路径才是主流吧!连 Path.GetFullPath 这种生成绝对路径的方法都已经成为 .NET Standard 的一部分了。

然而,生成相对路径依然有用——比如你的配置文件是相对于工作目录的,必须这个路径是输出给用户看的……

.NET Core 2.0 开始,提供了 Path.GetRelativePath 这样的方法来获取两个路径之间的相对路径,但是 .NET Framework 却没有这样的方法可以用。

那么,在旧版本的 .NET Core 或者 .NET Framework 中 Path 没有生成相对路径的方法,还能怎么生成相对路径呢?别跟我说自己去做字符串比较……

Uri 提供了 MakeRelativeUri 方法,可以生成一个路径到另一个路径的相对路径。于是我们可以写出这样的代码:

1 2 3 4 5 6 7 public static string MakeRelativePath(string fromPath, string toPath) { var fromUri = new Uri(fromPath); var toUri = new Uri(toPath); var relativeUri = fromUri.MakeRelativeUri(toUri); return Uri.UnescapeDataString(relativeUri.ToString()); }

运行传入 C:\Users\walterlv\OpenSource\Demo 和 C:\Users\walterlv\OpenSource\Demo\build\config.xml。结果,竟然得到的相对路径是:Demo/build/config.xml。

那个 Demo 明明是两者共有的路径部分,却存在于相对路径中;生成的路径使用 /,而不是 Windows 系统使用的 \。

于是我们需要分别进行这两个处理。对于前者,我们必须让 Uri 意识到这是一个文件夹才能让最终生成的路径不带这个重复的部分;对于后者,我们需要进行路径连接符转换。于是最终的代码我整理成了如下方法:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 public static string MakeRelativePath(string fromPath, string toPath) { if (string.IsNullOrEmpty(fromPath)) throw new ArgumentNullException(nameof(fromPath)); if (string.IsNullOrEmpty(toPath)) throw new ArgumentNullException(nameof(toPath)); var fromUri = new Uri(fromPath); var toUri = new Uri(toPath); if (fromUri.Scheme != toUri.Scheme) { // 不是同一种路径,无法转换成相对路径。 return toPath; } if (fromUri.Scheme.Equals("file", StringComparison.InvariantCultureIgnoreCase) && !fromPath.EndsWith("/", StringComparison.OrdinalIgnoreCase) && !fromPath.EndsWith("\\", StringComparison.OrdinalIgnoreCase)) { // 如果是文件系统,则视来源路径为文件夹。 fromUri = new Uri(fromPath + Path.DirectorySeparatorChar); } var relativeUri = fromUri.MakeRelativeUri(toUri); var relativePath = Uri.UnescapeDataString(relativeUri.ToString()); if (toUri.Scheme.Equals("file", StringComparison.InvariantCultureIgnoreCase)) { relativePath = relativePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); } return relativePath; }

现在重新传入 C:\Users\walterlv\OpenSource\Demo 和 C:\Users\walterlv\OpenSource\Demo\build\config.xml。结果,已经能够得到:build\config.xml 了。

关于 .NET Core 中的 Path.GetRelativePath 方法,可以参考:GetRelativePath

参考资料

.net - How to get relative path from absolute path - Stack OverflowPath.GetRelativePath

本文会经常更新,请阅读原文: https://blog.walterlv.com/post/make-relative-file-path.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

知识共享许可协议

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名 吕毅 (包含链接: https://blog.walterlv.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系 ([email protected]) 。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3