乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 10|回复: 0

[转帖]取得文件的短文件名称

[复制链接]

26

主题

589

帖子

10

银币

中流砥柱

Rank: 25

铜币
693
发表于 2004-3-19 22:27:00 | 显示全部楼层 |阅读模式
Get a Short Filename from a Long Filename
Step-by-Step Example
[ol]
  • Start Visual Basic. Form1 is created by default.
  • Place a CommandButton on Form1.
  • Place a Common Dialog control on the form.
  • From the Insert menu, select Module to add a single code module to the project.
  • Add the following code to 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
  • Add the following code to 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
  • Run the project by pressing the F5 key. Click on the Command button to show the Open dialog box. Navigate the Open dialog box and find a file that has a Long Filename. Select the file and click OK.
  • The message box will display the Long File name along with its Short File name. [/ol]
  • 回复

    使用道具 举报

    发表回复

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    • 微信公众平台

    • 扫描访问手机版

    • 点击图片下载手机App

    QQ|关于我们|小黑屋|乐筑天下 繁体中文

    GMT+8, 2025-8-15 12:59 , Processed in 3.901493 second(s), 54 queries .

    © 2020-2025 乐筑天下

    联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表