我有一个短版本或 DOS 格式的路径( "C:/DOCUME~1" 例如)并且想要获得它的完整路径/长路径( "C:/Documents And设置” 例如)。
我试过 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
我试图给 GetLongPathNameW 取别名,但它似乎什么都不做,对于 Unicode 和非 Unicode 文件名,总是返回 0。在 MSDN 中,只有关于 C/C++ 的 GetLongPathNameW 的文章,而不是 VB/VBA 的任何文章。我可以做错什么吗?
这种情况有什么解决办法吗?我在 Google 和 StackOverflow 上花了几个小时,但找不到。
问候,
最佳答案
这对你有用吗?我已将文件路径转换为短路径名,然后再次将其转换回来,即使使用 unicode(例如 C:/Tö+)也能提供正确的字符串
Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" _
(ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal lBuffer As Long) As Long
Private Declare Function GetLongPathName Lib "kernel32" Alias "GetLongPathNameA" _
(ByVal lpszShortPath As String, ByVal lpszLongPath As String, ByVal cchBuffer As Long) As Long
Public Function GetShortPath(ByVal strFileName As String) As String
'KPD-Team 1999
'URL: [url]http://www.allapi.net/[/url]
'E-Mail: [email]KPDTeam@Allapi.net[/email]
Dim lngRes As Long, strPath As String
'Create a buffer
strPath = String$(165, 0)
'retrieve the short pathname
lngRes = GetShortPathName(strFileName, strPath, 164)
'remove all unnecessary chr$(0)'s
GetShortPath = Left$(strPath, lngRes)
End Function
Public Function GetLongPath(ByVal strFileName As String) As String
Dim lngRes As Long, strPath As String
'Create a buffer
strPath = String$(165, 0)
'retrieve the long pathname
lngRes = GetLongPathName(strFileName, strPath, 164)
'remove all unnecessary chr$(0)'s
GetLongPath = Left$(strPath, lngRes)
End Function
Private Sub Test()
shortpath = GetShortPath("C:/Documents And Settings")
Longpath = GetLongPath(shortpath)
End Sub
关于vba - 获取带有 Unicode 文件名的完整路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11389069/