Win32 API 中 PathAppend 和 PathCombine 的区别

发布于 2024-10-05 07:13:59 字数 106 浏览 0 评论 0原文

我想了解这些函数之间有什么区别,以及我应该使用其中哪些函数来处理路径?

例如:我想要 "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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

灼疼热情 2024-10-12 07:13:59

如果字符串已经连接起来,PathCanonicalize() 可能值得一提。

PathCanonicalize() might be worth mentioning, in case the strings are already concatenated.

国际总奸 2024-10-12 07:13:59

您必须使用 PathCombine< /a> 为此。

连接两个字符串
表示正确形成的路径
一条路;还连接任何
相对路径元素。

PathAppend 特别根据 MSN 文档,排除相对路径限定符:

pszPath 中提供的路径不能
以“..\”或“.\”开头来生成
相对路径字符串。

You have to use PathCombine for this.

Concatenates two strings that
represent properly formed paths into
one path; also concatenates any
relative path elements.

PathAppend specifically rules out relative path qualifiers, per the MSN docs:

The path supplied in pszPath cannot
begin with "..\" or ".\" to produce
a relative path string.

千寻… 2024-10-12 07:13:59

假设:

lpStr1lpStr2lpStr3是三个指针,分别指向不同的字符串。

str1、str2str3std::string 类型的三个对象。

那么

PathCombine(lpStr1, lpStr2, lpStr3);

类似于

strcpy(lpStr1, lpStr2);
strcat(lpStr1, lpStr3);

类推

str1 = str2 + str3;

PathAppend(lpStr1, lpStr2);

类似于

strcat(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 and lpStr3 are three pointers, each points to a different string.

and

str1, str2 and str3 are three objects of type std::string.

Then

PathCombine(lpStr1, lpStr2, lpStr3);

is similar to

strcpy(lpStr1, lpStr2);
strcat(lpStr1, lpStr3);

that is analogy to

str1 = str2 + str3;

and

PathAppend(lpStr1, lpStr2);

is similar to

strcat(lpStr1, lpStr2);

that is analogy to

str1 += str2;

that is equivalent to

str1 = str1 + str2

Theoretically PathAppend can be implemented with PathCombine only:

PathAppend(lpStr1,lpStr2);

is equivalent to

PathCombine(lpStr1, lpStr1, lpStr2);

Therefore every task that can be accomplished by PathAppend can also be accomplished by PathCombine, but the contrary is not true.

Therefore as long as you can accomplish your task with PathAppend then use PathAppend.

If your task cannot be accomplished with PathAppend, but can be accomplished with PathCombine then use PathCombine.

If your task cannot be accomplished with PathCombine, surely it cannot be accomplished with PathAppend and you will have to use another API to accomplish your task.

The reason to always use PathAppend instead of PathCombine, if possible, is because PathAppend requires less parameters than PathCombine and it also shortens your code.

If you can solve your problem with PathAppend, but you use PathCombine instead then the first and second parameters in the call to PathCombine are the same and you probably duplicate code and type more characters in your code.

In this case the PathCombine line is longer than the PathAppend line and this makes your code also less readable.

So using PathAppend is always better and more productive than PathCombine as long as your problem can be solved with PathAppend.

Otherwise if your problem cannot be solved with PathAppend, but can be solved with PathCombine only then use PathCombine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文