问题
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?
回答1:
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.
回答2:
When you combine your two strings, you get the following.
C:\Users\userX\Music\Playlists\playlist.wpl\..\..\..\Public\Music\Sample Music\Amanda.wma
Since each "..
" will wipe out the preceding section, you end u[p with the following sequence:
C:\Users\userX\Music\Playlists\playlist.wpl\..\..\..\Public\Music\Sample Music\Amanda.wma
C:\Users\userX\Music\Playlists\..\..\Public\Music\Sample Music\Amanda.wma
C:\Users\userX\Music\..\Public\Music\Sample Music\Amanda.wma
C:\Users\userX\Public\Music\Sample Music\Amanda.wma
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.
来源:https://stackoverflow.com/questions/351479/getting-absolute-path-from-relative-in-vista-seems-to-fail-using-win32-shell-pat