VB6调用API打开目标文件所在文件夹且选中目标文件

Option Explicit

' 模块名称: mOpenFolderAndSetFileFocus
'     作者: 唐细刚
'     时间: 2010-08-22
'     功能: VB6调用API打开目标文件所在文件夹且选中目标文件
' 注:
' 由于调用 Explorer.exe /select 方式会使系统产生多余的 Explorer.exe 进程
' 所以还是API来实现较好,速度也有优势,不知道是否低碳? O(∩_∩)O~

Private Declare Function SHCreateFromPath Lib "Shell32" Alias "ILCreateFromPathA" (ByVal lpFileName As String) As Long
Private Declare Sub SHFree Lib "Shell32" Alias "ILFree" (ByVal lngPidl As Long)
Private Declare Function SHOpenFolderAndSelectItems Lib "Shell32" ( _
        ByVal pidlFolder As Long, _
        ByVal cidl As Long, _
        ByVal apidl As Long, _
        ByVal dwFlags As Long) As Long

'增加判断文件是否存在
Private Const INVALID_HANDLE_VALUE = -1
Private Const MAX_PATH = 260

Private Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type

Private Type WIN32_FIND_DATA
    dwFileAttributes As Long
    ftCreationTime As FILETIME
    ftLastAccessTime As FILETIME
    ftLastWriteTime As FILETIME
    nFileSizeHigh As Long
    nFileSizeLow As Long
    dwReserved0 As Long
    dwReserved1 As Long
    cFileName As String * MAX_PATH
    cAlternate As String * 14
End Type

Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" ( _
    ByVal lpFileName As String, _
    lpFindFileData As WIN32_FIND_DATA) As Long

Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long

Private Function FileExists(ByVal lpFileName As String) As Boolean
    Dim tpWFD As WIN32_FIND_DATA
    Dim lngFile As Long
    lngFile = FindFirstFile(lpFileName, tpWFD)
    FileExists = lngFile <> INVALID_HANDLE_VALUE
    If lngFile Then Call FindClose(lngFile)
End Function

'调用成功返回 True,否则返回 False
Public Function OpenFolderAndSetFileFocus(ByVal lpFileName As String) As Boolean
    On Error Resume Next
    Dim lngPidl As Long
    Dim lngRet  As Long
    Dim strFile As String
    strFile = Trim(lpFileName)
    If FileExists(strFile) = False Then Exit Function
    lngPidl = SHCreateFromPath(strFile & vbNullChar)
    If lngPidl <> 0 Then
       lngRet = SHOpenFolderAndSelectItems(lngPidl, 0, 0, 0)
       If lngRet = 0 Then
          OpenFolderAndSetFileFocus = True
       End If
       Call SHFree(lngPidl)
    End If
End Function


 

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值