明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2091|回复: 0

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

[复制链接]
发表于 2004-3-19 22:27:00 | 显示全部楼层 |阅读模式
Get a Short Filename from a Long Filename

Step-by-Step Example

  1. Start Visual Basic. Form1 is created by default.
  2. Place a CommandButton on Form1.
  3. Place a Common Dialog control on the form.
  4. From the Insert menu, select Module to add a single code module to the project.
  5. 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
    
    
  6. 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
    
    
  7. 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.
  8. The message box will display the Long File name along with its Short File name.
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-29 00:47 , Processed in 0.169638 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表