vba - 获取带有 Unicode 文件名的完整路径

标签 vba unicode path

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

相关文章:

excel - 删除 VBA 按钮集合

ms-access - 根据数量字段重复记录

vba - 有没有一种方法可以使用与 VBA 的默认分隔符 (vbCr/vbCrLf) 不同的分隔符一次一行地读取文件?

c++ - 如何在资源文件中添加 Ⓒ 符号以支持日文 Windows 操作系统。

python - 如何读取包含16位十六进制值的文件?

node.js - 如何在nodejs中定义文件路径需要('')和 "../"符号?

mysql - 在 Windows 8.1 上覆盖路径内容 - 环境变量

excel - 将动态超链接函数设为静态

string - Swift - 用空格替换字符串中的表情符号

animation - 为什么这个 SVG 动画不起作用