【问题标题】:Get full path with Unicode file name获取带有 Unicode 文件名的完整路径
【发布时间】:2012-07-09 04:25:37
【问题描述】:

我有一个简短版本或 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

我尝试为 GetLongPathNameW 取别名,但它似乎什么也没做,对于 Unicode 和非 Unicode 文件名,总是返回 0。在 MSDN 中只有一篇关于 C/C++ 的 GetLongPathNameW 的文章,而不是任何 VB/VBA 的文章。我可以做错什么吗?

这种情况有什么解决办法吗?我在 Google 和 StackOverflow 上花了几个小时,但找不到。

问候,

【问题讨论】:

  • 我从文档中注意到 unicode 必须以 \\?\ 开头,因此您的示例将变为 \\?\C:\DOCUME~1
  • @SeanCheshire 感谢您的建议。但是我尝试了几次测试,它根本不起作用。这是结果 strPath = "\\?\" & strPath ?GetLongPathName(strPath, strTemp, 255) 0

标签: vba unicode path


【解决方案1】:

这对你有用吗?我已将文件路径转换为短路径名,然后再次将其转换回来,即使是 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

【讨论】:

    【解决方案2】:

    要使用 vb6/vba 中的 W 函数,您将所有字符串参数声明为 long:

    Private Declare Function GetLongPathName Lib "kernel32" Alias "GetLongPathNameW" _
      (ByVal lpszShortPath As Long, _
       ByVal lpszLongPath As Long, _
       ByVal cchBuffer As Long) As Long
    

    并传递StrPtr(a_string) 而不仅仅是a_string

    如果你有:

    dim s_path as string
    dim l_path as string
    
    s_path = "C:\DOCUME~1"
    l_path = string$(1024, vbnullchar)
    
    GetLongPathNameA s_path, l_path, len(l_path)
    

    会变成

    dim s_path as string
    dim l_path as string
    
    s_path = "C:\DOCUME~1"
    l_path = string$(1024, vbnullchar)
    
    GetLongPathNameW strptr(s_path), strptr(l_path), len(l_path)
    

    【讨论】:

    • 我想我在挣扎时尝试过这种方法。但我会再考虑一下。
    猜你喜欢
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    相关资源
    最近更新 更多