[转帖]取得文件的短文件名称
Get a Short Filename from a Long Filename<FONT face=Verdana size=1>
<H3>Step-by-Step Example</H3>
<OL>
<LI><FONT size=2>Start Visual Basic. Form1 is created by default. </FONT>
<LI><FONT size=2>Place a CommandButton on Form1. </FONT>
<LI><FONT size=2>Place a Common Dialog control on the form. </FONT>
<LI><FONT size=2>From the Insert menu, select Module to add a single code module to the project. </FONT>
<LI><FONT size=2>Add the following code to Module1: </FONT>
<PRE><FONT size=2> 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
</FONT></PRE>
<LI><FONT size=2>Add the following code to Form1: </FONT>
<PRE><FONT size=2> 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
</FONT></PRE>
<LI><FONT size=2>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. </FONT>
<LI><FONT size=2>The message box will display the Long File name along with its Short File name. </FONT></LI></OL></FONT>
页:
[1]