获取Unicode文件名的完整路径

Shi*_*mae 5 unicode vba path

我有一个短版本或DOS格式的路径(例如"C:/ DOCUME~1")并希望得到它的完整路径/长路径(例如"C:/ Documents And Settings").

我试过GetLongPathName api.有效.但是当处理unicode文件名时,它就会失败.

Private Declare Function GetLongPathName Lib "kernel32" Alias _
    "GetLongPathNameA" (ByVal lpszShortPath As String, _
    ByVal lpszLongPath As String, ByVal cchBuffer As Long) As Long
Run Code Online (Sandbox Code Playgroud)

我尝试使用别名GetLongPathNameW,但似乎什么都不做,因为BOTH Unicode和非Unicode文件名,总是返回0.在MSDN中,只有关于C/C++的GetLongPathNameW的文章,而不是任何用于VB/VBA的文章.我可能做错了吗?

这种情况有什么解决方案吗?我在Google和StackOverflow上花了好几个小时但却找不到.

问候,

小智 4

这对你有用吗?我已将文件路径转换为短路径名,然后再次将其转换回来,即使在使用 unicode 时也能给出正确的字符串(例如 C:/T\xc3\xb6+)

\n\n
Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" _\n    (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal lBuffer As Long) As Long\nPrivate Declare Function GetLongPathName Lib "kernel32" Alias "GetLongPathNameA" _\n    (ByVal lpszShortPath As String, ByVal lpszLongPath As String, ByVal cchBuffer As Long) As Long\n\nPublic Function GetShortPath(ByVal strFileName As String) As String\n    \'KPD-Team 1999\n    \'URL: [url]http://www.allapi.net/[/url]\n    \'E-Mail: [email]KPDTeam@Allapi.net[/email]\n    Dim lngRes As Long, strPath As String\n    \'Create a buffer\n    strPath = String$(165, 0)\n    \'retrieve the short pathname\n    lngRes = GetShortPathName(strFileName, strPath, 164)\n    \'remove all unnecessary chr$(0)\'s\n    GetShortPath = Left$(strPath, lngRes)\nEnd Function\n\nPublic Function GetLongPath(ByVal strFileName As String) As String\n    Dim lngRes As Long, strPath As String\n    \'Create a buffer\n    strPath = String$(165, 0)\n    \'retrieve the long pathname\n    lngRes = GetLongPathName(strFileName, strPath, 164)\n    \'remove all unnecessary chr$(0)\'s\n    GetLongPath = Left$(strPath, lngRes)\nEnd Function\n\nPrivate Sub Test()\n    shortpath = GetShortPath("C:/Documents And Settings")\n\n    Longpath = GetLongPath(shortpath)\nEnd Sub\n
Run Code Online (Sandbox Code Playgroud)\n