Win32 API 中 PathAppend 和 PathCombine 的区别
我想了解这些函数之间有什么区别,以及我应该使用其中哪些函数来处理路径?
例如:我想要 "C:\Temp" + "..\Folder" = "C:\Folder"
谢谢
I want to understand what's the difference between those functions, and which of them should I use for work with paths?
For example: I want "C:\Temp" + "..\Folder" = "C:\Folder"
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果字符串已经连接起来,
PathCanonicalize()
可能值得一提。PathCanonicalize()
might be worth mentioning, in case the strings are already concatenated.您必须使用
PathCombine
< /a> 为此。PathAppend
特别根据 MSN 文档,排除相对路径限定符:You have to use
PathCombine
for this.PathAppend
specifically rules out relative path qualifiers, per the MSN docs:假设:
lpStr1
、lpStr2
和lpStr3
是三个指针,分别指向不同的字符串。str1、
str2
和str3
是std::string
类型的三个对象。那么
类似于
类推
str1 = str2 + str3;
和
PathAppend(lpStr1, lpStr2);
类似于
类推
str1 += str2 ;
相当于
str1 = str1 + str2
理论上,
PathAppend
只能用PathCombine
实现:PathAppend(lpStr1 ,lpStr2);
等价于
PathCombine(lpStr1, lpStr1, lpStr2);
因此,可以通过
PathAppend
完成的所有任务也可以通过完成>PathCombine
,但反之则不然。因此,只要您可以使用
PathAppend
完成任务,那么就使用PathAppend
。如果您的任务无法使用
PathAppend
完成,但可以使用PathCombine
完成,则使用PathCombine
。如果您的任务无法使用
PathCombine
完成,那么当然也无法使用PathAppend
完成,您将不得不使用另一个 API 来完成您的任务。如果可能,始终使用
PathAppend
而不是PathCombine
的原因是PathAppend
需要的参数比少>PathCombine
并且它还可以缩短您的代码。如果您可以使用
PathAppend
解决问题,但使用PathCombine
代替,则调用PathCombine
时的第一个和第二个参数是相同的,并且您可能会重复代码并在代码中键入更多字符。在这种情况下,
PathCombine
行比PathAppend
行长,这也会使您的代码可读性较差。因此,只要您的问题可以通过
PathAppend
解决,那么使用PathAppend
总是比PathCombine
更好、更高效。否则,如果您的问题无法通过 PathAppend 解决,但可以通过 PathCombine 解决,则只能使用 PathCombine。
Assume that:
lpStr1
,lpStr2
andlpStr3
are three pointers, each points to a different string.and
str1
,str2
andstr3
are three objects of typestd::string
.Then
is similar to
that is analogy to
str1 = str2 + str3;
and
PathAppend(lpStr1, lpStr2);
is similar to
that is analogy to
str1 += str2;
that is equivalent to
str1 = str1 + str2
Theoretically
PathAppend
can be implemented withPathCombine
only:PathAppend(lpStr1,lpStr2);
is equivalent to
PathCombine(lpStr1, lpStr1, lpStr2);
Therefore every task that can be accomplished by
PathAppend
can also be accomplished byPathCombine
, but the contrary is not true.Therefore as long as you can accomplish your task with
PathAppend
then usePathAppend
.If your task cannot be accomplished with
PathAppend
, but can be accomplished withPathCombine
then usePathCombine
.If your task cannot be accomplished with
PathCombine
, surely it cannot be accomplished withPathAppend
and you will have to use another API to accomplish your task.The reason to always use
PathAppend
instead ofPathCombine
, if possible, is becausePathAppend
requires less parameters thanPathCombine
and it also shortens your code.If you can solve your problem with
PathAppend
, but you usePathCombine
instead then the first and second parameters in the call toPathCombine
are the same and you probably duplicate code and type more characters in your code.In this case the
PathCombine
line is longer than thePathAppend
line and this makes your code also less readable.So using
PathAppend
is always better and more productive thanPathCombine
as long as your problem can be solved withPathAppend
.Otherwise if your problem cannot be solved with
PathAppend
, but can be solved withPathCombine
only then usePathCombine
.