windows 获取相对于特定目录的文件路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1672561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:22:57  来源:igfitidea点击:

Getting a file path relative to a particular directory

c++windowswinapi

提问by Benj

Is there a nice way in the WinAPI to get a path relative to another path? For example:

WinAPI 中是否有一种很好的方法来获取相对于另一条路径的路径?例如:

Have a list of paths relative to:

有一个相对于以下的路径列表:

a\b\c\d

A B C D

And I'd like to change them all to paths relative to

我想将它们全部更改为相对于的路径

a\b\c

a\b\c

Is there a neat way to do this or should I get path parsing?

有没有一种巧妙的方法可以做到这一点,还是应该进行路径解析?

回答by Georg Fritzsche

As far as i know there is no such functionality in the windows API, but there is e.g. an article on CodeGuruthat provides that functionality.

据我所知,windows API 中没有这样的功能,但是有一篇关于 CodeGuru文章提供了该功能。

edit:
I stand corrected, there is PathRelativePathTo()in the shell api as mentioned in this answer.

编辑
我已纠正,如本答案中所述,shell api 中有PathRelativePathTo()

回答by the_mandrill

I don't know of any way of doing this directly, but you might be able to do it in a couple of steps using PathCommonPrefixand PathCombine. Otherwise, boost::filesystemmay make it possible, eg with the relative_path()method.

我不知道有什么方法可以直接执行此操作,但您可以使用PathCommonPrefixPathCombine 分几步完成。否则,boost::filesystem可能使它成为可能,例如使用该relative_path()方法。