获取带有Unicode文件名的完整路径 [英] Get full path with Unicode file name

查看:104
本文介绍了获取带有Unicode文件名的完整路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简短版本或DOS格式的路径(例如,"C:/DOCUME〜1" ),并且想要获取它的完整路径/长路径("C :/Documents and Settings" (例如).

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" e.g).

我尝试了GetLongPathName api.有效.但是,当处理unicode文件名时,结果就是失败.

I tried GetLongPathName api. It WORKED. But when deal with unicode filename it turns out failure.

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则没有.我可以做错什么吗?

I tried to alias GetLongPathNameW instead but it seems do nothing, for BOTH Unicode and non-Unicode filename, always return 0. In MSDN there's only article about GetLongPathNameW for C/C++, not any for VB/VBA. May I do something wrong?

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

Is there any solution for this case? I spend hours on Google and StackOverflow but can't find out.

此致

推荐答案

这对您有用吗?我已经将文件路径转换为短路径名,然后再次将其转换回,即使使用Unicode(例如C:/Tö+),它也能提供正确的字符串

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

这篇关于获取带有Unicode文件名的完整路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆