Get full path with Unicode file name

前端 未结 2 1303
被撕碎了的回忆
被撕碎了的回忆 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
    

提交回复
热议问题