使用 Win32/Shell PathCombine() 从 Vista 中的相对路径获取绝对路径似乎失败
不确定这是否是预期的行为或错误或我正在使用的错误函数,但问题是 PathCombine() 在 Vista 盒子上返回错误的路径。
相对路径为(由 WMP 导出到播放列表):
..\..\..\Public\Music\Sample Music\Amanda.wma
它的相对路径为:
C:\Users\userX\Music\Playlists\playlist.wpl
和 PathCombine() 返回:
C:\Users\userX\Public\Music\Sample Music\Amanda.wma
但是,该文件实际上位于此处(根据资源管理器判断以及我无法从代码中打开它的事实):
C:\Users\Public\Music\Sample Music\Amanda.wma
是这是一个已知问题? 我还应该使用其他功能吗?
Not sure if this is intended behavior or a bug or a wrong function that I'm using, but the problem is that PathCombine() returns a wrong path on a Vista box.
The relative path is (as exported by the WMP to a playlist):
..\..\..\Public\Music\Sample Music\Amanda.wma
The path it's relative to is:
C:\Users\userX\Music\Playlists\playlist.wpl
and PathCombine() returns:
C:\Users\userX\Public\Music\Sample Music\Amanda.wma
however, the file is actually located here (judging by the Explorer and the fact that I can't open it from the code):
C:\Users\Public\Music\Sample Music\Amanda.wma
Is this a known issue? Is there some other function I should be using?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PathCombine 文档 指定第二个参数, lpszDir,是“指向包含目录路径的最大长度 MAX_PATH 的以 null 结尾的字符串的指针。” 您似乎正在传递目录内文件的完全限定名称,而不是目录的完全限定名称。 因此,它删除三个组件:playlist.wpl、播放列表和音乐,然后附加其余部分。
您应该能够使用 PathRemoveFileSpec 删除目录路径中的文件部分。
The documentation for PathCombine specifies that the second parameter, lpszDir, is "A pointer to a null-terminated string of maximum length MAX_PATH that contains the directory path." You appear to be passing the fully qualified name of a file inside the directory, instead of the fully qualified name of the directory. So, it removes three components: playlist.wpl, Playlists, and Music, and then appends the remainder.
You should be able to use PathRemoveFileSpec to remove the file part from your directory path.
当您组合两个字符串时,您会得到以下结果。
由于每个“
..
”都会清除前面的部分,因此您可以使用以下序列结束 u[p:这是因为“
PathCombine()
”不会被是否有任何路径的各个部分是文件或目录。 这只是一种相对愚蠢的方式,将特殊导航字符(“..
”和“.
”)与真实导航段进行匹配,以形成没有这些特殊导航字符的路径。它只是假设“
playlist.wpl”是您的情况下的目录名称。 删除它(或在相对路径的开头添加另一个“
..
”,这是避免删除文件名部分的无关代码的技巧),它应该可以正常工作。When you combine your two strings, you get the following.
Since each "
..
" will wipe out the preceding section, you end u[p with the following sequence:That's because "
PathCombine()
" is not bothered by whether any segments of your path are files or directories. It's just a relatively dumb way of matching special navigation characters ("..
" and ".
") against real navigation segments to form a path without those special navigation characters.It's just assuming that "
playlist.wpl
" is a directory name in your case. Strip that off (or add another "..
" at the start of your relative path, a trick to avoid extraneous code for stripping of the filename section) and it should work okay.