- 积分
- 10513
- 明经币
- 个
- 注册时间
- 2002-6-3
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
Get a Short Filename from a Long Filename
Step-by-Step Example
- 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.
|
|