Public Declare Function ShellExecute Lib "shell32.dll" Alias _ "ShellExecuteA" _ (ByVal hWnd As Long, ByVal lpOperation As String, _ ByVal lpFile As String, _ ByVal lpParameters As String, _ ByVal lpDirectory As String, _ ByVal nShowCmd As Long) As LongPublic Const SW_SHOWNORMAL As Long = 1 Public Const SW_HIDE As Long = 0Call ShellExecute(Me.hWnd, "Open", "C:\Documents and Settings\user1\My Documents\BLPS_Init.mdb", vbNullString, _ App.Path, SW_SHOWNORMAL)'-------------------------------------------------------------------------------------------- Dim ObjName as FileSystemObject ObjName.GetParentFolderName(FileName) '-----------------------------------------------------------------------------------------------GetModuleFileNamePrivate Declare Function GetModuleFileName Lib "kernel32" Alias _ "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As _ String, ByVal nSize As Long) As LongFunction IsRunUnderVB5() As Boolean Dim S As String, Length Length = 256 S = String(Length, 0) Call GetModuleFileName(0, S, Length) S = Left(S, InStr(S, Chr(0)) - 1) IsRunUnderVB5 = UCase(Right(S, 7)) = "VB5.EXE"End Function'---------------------不过我也找到了一个函数GetLongPathName(),用这个可以一次把路径搞定,然后用 FindFirstFile把文件名搞定,然后组合就可以了。 Dim sLongName As String Dim sTemp As String Dim iSlashPos As Integer Dim sShortName As String
'Add \ to short name to prevent Instr from failing sShortName = sFilename & "\"
'Start from 4 to ignore the "[Drive Letter]:\" characters iSlashPos = InStr(4, sShortName, "\")
'Pull out each string between \ character for conversion While iSlashPos sTemp = Dir(Left$(sShortName, iSlashPos - 1), _ vbNormal + vbHidden + vbSystem + vbDirectory) If sTemp = "" Then 'Error 52 - Bad File Name or Number LongFilename = "" Exit Property End If sLongName = sLongName & "\" & sTemp iSlashPos = InStr(iSlashPos + 1, sShortName, "\") Wend
'Prefix with the drive letter LongFilename = Left$(sShortName, 2) & sLongName Public Declare Function GetFullPathName Lib "kernel32" Alias "GetFullPathNameA" (ByVal lpFileName As String, ByVal nBufferLength As Long, ByVal lpBuffer As String, ByVal lpFilePart As String) As LongPublic Function toLongName(ByVal FileName As String) As String Dim TokenStr As New Collection, pos As Integer Dim FullPathName As String, I As Long Dim ptstr As String ptstr = String(256, 0) FullPathName = String(256, 0)'先取得 檔案/目錄 的完整目錄名稱 I = GetFullPathName(FileName, 256, FullPathName, ptstr) FullPathName = Left(FullPathName, InStr(1, FullPathName, Chr(0)) - 1) '如果該 檔案/目錄不存在則返回 If Len(Dir(FullPathName, vbDirectory + vbNormal + vbHidden + vbSystem + vbReadOnly)) = 0 Then toLongName = "" Exit Function End If '取得FullPtahName各個部份,如 C:\DIRECT~1\FILENAME '將變成 C: DIRECT~1 FILENAME三個字串存在TokenStr '的Collection中 Do While True pos = InStr(1, FullPathName, "\") If pos <> 0 Then TokenStr.Add Left(FullPathName, pos - 1) FullPathName = Mid(FullPathName, pos + 1) Else TokenStr.Add FullPathName Exit Do End If Loop '取出各個Token,並以Dir指令取得 檔案/目錄 的長檔名 toLongName = TokenStr(1) + "\" '第一個一定是Driver名稱(如 c:) Dim LongName As String For I = 2 To TokenStr.Count 'Dir("C:\Progra~1") 會傳回 "Program Files" LongName = Dir(toLongName + TokenStr(I), vbNormal + vbSystem + vbArchive + vbDirectory + vbHidden) toLongName = toLongName + LongName + "\" Next toLongName = Left(toLongName, Len(toLongName) - 1) End Function 方法二 說明 這是透過Dir函數去做的,和方法一有些類似程式 Public Function GetLongFileName(ByVal ShortFileName As String) As String
Dim intPos As Integer Dim strLongFileName As String Dim strDirName As String
'Format the filename for later processing ShortFileName = ShortFileName & "\"
'Grab the position of the first real slash intPos = InStr(4, ShortFileName, "\")
'Loop round all the directories and files 'in ShortFileName, grabbing the full names 'of everything within it.
"ShellExecuteA" _
(ByVal hWnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As LongPublic Const SW_SHOWNORMAL As Long = 1
Public Const SW_HIDE As Long = 0Call ShellExecute(Me.hWnd, "Open", "C:\Documents and Settings\user1\My Documents\BLPS_Init.mdb", vbNullString, _
App.Path, SW_SHOWNORMAL)'--------------------------------------------------------------------------------------------
Dim ObjName as FileSystemObject
ObjName.GetParentFolderName(FileName)
'-----------------------------------------------------------------------------------------------GetModuleFileNamePrivate Declare Function GetModuleFileName Lib "kernel32" Alias _
"GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As _
String, ByVal nSize As Long) As LongFunction IsRunUnderVB5() As Boolean Dim S As String, Length
Length = 256
S = String(Length, 0)
Call GetModuleFileName(0, S, Length)
S = Left(S, InStr(S, Chr(0)) - 1)
IsRunUnderVB5 = UCase(Right(S, 7)) = "VB5.EXE"End Function'---------------------不过我也找到了一个函数GetLongPathName(),用这个可以一次把路径搞定,然后用
FindFirstFile把文件名搞定,然后组合就可以了。 Dim sLongName As String
Dim sTemp As String
Dim iSlashPos As Integer
Dim sShortName As String
'Add \ to short name to prevent Instr from failing
sShortName = sFilename & "\"
'Start from 4 to ignore the "[Drive Letter]:\" characters
iSlashPos = InStr(4, sShortName, "\")
'Pull out each string between \ character for conversion
While iSlashPos
sTemp = Dir(Left$(sShortName, iSlashPos - 1), _
vbNormal + vbHidden + vbSystem + vbDirectory)
If sTemp = "" Then
'Error 52 - Bad File Name or Number
LongFilename = ""
Exit Property
End If
sLongName = sLongName & "\" & sTemp
iSlashPos = InStr(iSlashPos + 1, sShortName, "\")
Wend
'Prefix with the drive letter
LongFilename = Left$(sShortName, 2) & sLongName Public Declare Function GetFullPathName Lib "kernel32" Alias "GetFullPathNameA" (ByVal lpFileName As String, ByVal nBufferLength As Long, ByVal lpBuffer As String, ByVal lpFilePart As String) As LongPublic Function toLongName(ByVal FileName As String) As String
Dim TokenStr As New Collection, pos As Integer
Dim FullPathName As String, I As Long
Dim ptstr As String
ptstr = String(256, 0)
FullPathName = String(256, 0)'先取得 檔案/目錄 的完整目錄名稱
I = GetFullPathName(FileName, 256, FullPathName, ptstr)
FullPathName = Left(FullPathName, InStr(1, FullPathName, Chr(0)) - 1)
'如果該 檔案/目錄不存在則返回
If Len(Dir(FullPathName, vbDirectory + vbNormal + vbHidden + vbSystem + vbReadOnly)) = 0 Then
toLongName = ""
Exit Function
End If
'取得FullPtahName各個部份,如 C:\DIRECT~1\FILENAME
'將變成 C: DIRECT~1 FILENAME三個字串存在TokenStr
'的Collection中
Do While True
pos = InStr(1, FullPathName, "\")
If pos <> 0 Then
TokenStr.Add Left(FullPathName, pos - 1)
FullPathName = Mid(FullPathName, pos + 1)
Else
TokenStr.Add FullPathName
Exit Do
End If
Loop
'取出各個Token,並以Dir指令取得 檔案/目錄 的長檔名
toLongName = TokenStr(1) + "\" '第一個一定是Driver名稱(如 c:)
Dim LongName As String
For I = 2 To TokenStr.Count
'Dir("C:\Progra~1") 會傳回 "Program Files"
LongName = Dir(toLongName + TokenStr(I), vbNormal + vbSystem + vbArchive + vbDirectory + vbHidden)
toLongName = toLongName + LongName + "\"
Next
toLongName = Left(toLongName, Len(toLongName) - 1)
End Function 方法二 說明 這是透過Dir函數去做的,和方法一有些類似程式 Public Function GetLongFileName(ByVal ShortFileName As String) As String
Dim intPos As Integer
Dim strLongFileName As String
Dim strDirName As String
'Format the filename for later processing
ShortFileName = ShortFileName & "\"
'Grab the position of the first real slash
intPos = InStr(4, ShortFileName, "\")
'Loop round all the directories and files
'in ShortFileName, grabbing the full names
'of everything within it.
While intPos
strDirName = Dir(Left(ShortFileName, intPos - 1), _
vbNormal + vbHidden + vbSystem + vbDirectory)
If strDirName = "" Then
GetLongFileName = ""
Exit Function
End If
strLongFileName = strLongFileName & "\" & strDirName
intPos = InStr(intPos + 1, ShortFileName, "\")
Wend
'Return the completed long file name
GetLongFileName = Left(ShortFileName, 2) & strLongFileName
End Function