Get full path with Unicode file name

前端 未结 2 1284
被撕碎了的回忆
被撕碎了的回忆 2020-12-20 22:37

I have a path in short version or in DOS format (\"C:/DOCUME~1\" e.g) and want to get the full path/long path of it (\"C:/Documents And Settings\"

相关标签:
2条回答
  • 2020-12-20 23:12

    Does this work for you? I've converted the file path to short path name then converted it back again which gives the correct string even when unicode (eg 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
    
    0 讨论(0)
  • 2020-12-20 23:18

    To use W-functions from vb6/vba, you declare all string parameters as long:

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

    and pass StrPtr(a_string) instead of just a_string.

    So if you had:

    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)
    

    it would become

    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)
    
    0 讨论(0)
提交回复
热议问题