开发者

Difference between PathAppend and PathCombine in Win32 API

开发者 https://www.devze.com 2023-01-28 09:41 出处:网络
I want to understand what\'s the difference between those functions, and which of them should I use for work with paths?

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


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消