请知悉:本文最近一次更新为 11年 前,文中内容可能已经过时。

分步示例
在 Visual Basic 中创建一个新的标准 EXE 项目。
默认情况下,将创建 Form1。
在 Form1 中放置命令按钮控件。
放置在窗体上的一个通用对话框控件。

  1. 插入菜单中,选择要添加到项目的单个代码模块的模块
  2. 将下面的代码添加到 Module1 中:
    Declare Function GetShortPathName Lib "kernel32" _
    Alias "GetShortPathNameA" (ByVal lpszLongPath As String, _
    ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
    Public Function GetShortName(ByVal sLongFileName As String) As String
    Dim lRetVal As Long, sShortPathName As String, iLen As Integer
    'Set up buffer area for API function call return
    sShortPathName = Space(255)
    iLen = Len(sShortPathName)
    'Call the function
    lRetVal = GetShortPathName(sLongFileName, sShortPathName, iLen)
    'Strip away unwanted characters.
    GetShortName = Left(sShortPathName, lRetVal)
    End Function
  3. 向 Form1 中添加以下代码:
    Private Sub Command1_Click()
    Dim msg As String
    CommonDialog1.FileName = "*.*"
    CommonDialog1.ShowOpen
    msg = "Long File Name: " & CommonDialog1.filename & vbCrLf
    msg = msg & "Short File Name: " & GetShortName(CommonDialog1.filename)
    MsgBox msg
    End Sub
  4. 通过按 F5 键运行该项目。单击命令按钮以显示打开对话框。定位打开对话框并找到具有长文件名的文件。选择该文件,然后单击确定。
  5. 消息框将显示以及其短文件名的长文件名。

以上内容来自:https://support.microsoft.com/kb/175512/zh-cn


如您从本文得到了有价值的信息或帮助,请考虑扫描文末二维码捐赠和鼓励。

尊重他人劳动成果。转载请务必附上原文链接,我将感激不尽。


与《VB-取短文件名路径》相关的博文:


留言

Admin Avatar
😀
😀😁😂😅😭🤭😋😘🤔😰😱🤪💪👍👎🤝🌹👌