VB 监视文件改变 代码

Attribute VB_Name  =   " mShellNotify "
Option   Explicit

'  Brought to you by Brad Martinez
'
   http://members.aol.com/btmtz/vb
'
   http://www.mvps.org/ccrp

'  Code was written in and formatted for 8pt MS San Serif

'  ====================================================================
'
 Demonstrates how to receive shell change notifications (ala "what happens when the
'
 SHChangeNotify API is called?")

'  Interpretation of the shell's undocumented functions SHChangeNotifyRegister (ordinal 2)
'
 and SHChangeNotifyDeregister (ordinal 4) would not have been possible without the
'
 assistance of James Holderness. For a complete (and probably more accurate) overview
'
 of shell change notifcations, please refer to James' "Shell Notifications" page at
'
 http://www.geocities.com/SiliconValley/4942/
'
 ====================================================================

Private  m_hSHNotify  As   Long        '  the one and only shell change notification handle for the desktop folder
Private  m_pidlDesktop  As   Long     '  the desktop's pidl

'  User defined notiication message sent to the specified window's window proc.
Public   Const  WM_SHNOTIFY  =   & H401

'  ====================================================================

Public  Type PIDLSTRUCT
  
'  Fully qualified pidl (relative to the desktop folder) of the folder to monitor changes in.
   '  0 can also be specifed for the desktop folder.
  pidl  As   Long
  
'  Value specifying whether changes in the folder's subfolders trigger a change notification
   '   event (it's actually a Boolean, but we'll go Long because of VB's DWORD struct alignment).
  bWatchSubFolders  As   Long
End  Type

Declare   Function SHChangeNotifyRegister Lib "shell32" Alias "#2" _
                              (
ByVal hWnd As Long, _
                              
ByVal uFlags As SHCN_ItemFlags, _
                              
ByVal dwEventID As SHCN_EventIDs, _
                              
ByVal uMsg As Long, _
                              
ByVal cItems As Long, _
                              lpps 
As PIDLSTRUCT) As Long

' hWnd        - Handle of the window to receive the window message specified in uMsg.

' uFlags        - Flag that indicates the meaning of the dwItem1 and dwItem2 members of the
'
                     SHNOTIFYSTRUCT (which is pointed to by the window procedure's wParam
'
                     value when the specifed window message is received). This parameter can
'
                     be one of the SHCN_ItemFlags enum values below.
'
                     This interpretaion may be inaccurate as it appears pdils are almost alway returned
'
                     in the SHNOTIFYSTRUCT. See James' site for more info...

' dwEventId - Combination of SHCN_EventIDs enum values that specifies what events the
'
                     specified window will be notified of. See below.
                      
' uMsg          - Window message to be used to identify receipt of a shell change notification.
'
                      The message should *not* be a value that lies within the specifed window's
'
                      message range ( i.e. BM_ messages for a button window) or that window may
'
                      not receive all (if not any) notifications sent by the shell!!!

' cItems         - Count of PIDLSTRUCT structures in the array pointed to by the lpps param.

' lpps             - Pointer to an array of PIDLSTRUCT structures indicating what folder(s) to monitor
'
                      changes in, and whether to watch the specified folder's subfolder.

' If successful, returns a notification handle which must be passed to SHChangeNotifyDeregister
'
 when no longer used. Returns 0 otherwise.

' Once the specified message is registered with SHChangeNotifyRegister, the specified
'
 window's function proc will be notified by the shell of the specified event in (and under)
'
 the folder(s) speciifed in apidl. On message receipt, wParam points to a SHNOTIFYSTRUCT
'
 and lParam contains the event's ID value.

' The values in dwItem1 and dwItem2 are event specific. See the description of the values
'
 for the wEventId parameter of the documented SHChangeNotify API function.
Type SHNOTIFYSTRUCT
  dwItem1 
As Long
  dwItem2 
As Long
End Type

' ...?
'
Declare Function SHChangeNotifyUpdateEntryList Lib "shell32" Alias "#5" _
'
                              (ByVal hNotify As Long, _
'
                              ByVal Unknown As Long, _
'
                              ByVal cItem As Long, _
'
                              lpps As PIDLSTRUCT) As Boolean
'
'
Declare Function SHChangeNotifyReceive Lib "shell32" Alias "#5" _
'
                              (ByVal hNotify As Long, _
'
                              ByVal uFlags As SHCN_ItemFlags, _
'
                              ByVal dwItem1 As Long, _
'
                              ByVal dwItem2 As Long) As Long

' Closes the notification handle returned from a call to SHChangeNotifyRegister.
'
 Returns True if succeful, False otherwise.
Declare Function SHChangeNotifyDeregister Lib "shell32" Alias "#4" (ByVal hNotify As LongAs Boolean

' ====================================================================

' This function should be called by any app that changes anything in the shell.
'
 The shell will then notify each "notification registered" window of this action.
Declare Sub SHChangeNotify Lib "shell32" _
                        (
ByVal wEventId As SHCN_EventIDs, _
                        
ByVal uFlags As SHCN_ItemFlags, _
                        
ByVal dwItem1 As Long, _
                        
ByVal dwItem2 As Long)

' Shell notification event IDs

Public Enum SHCN_EventIDs
  SHCNE_RENAMEITEM 
= &H1      ' (D) A nonfolder item has been renamed.
  SHCNE_CREATE = &H2                ' (D) A nonfolder item has been created.
  SHCNE_DELETE = &H4                ' (D) A nonfolder item has been deleted.
  SHCNE_MKDIR = &H8                  ' (D) A folder item has been created.
  SHCNE_RMDIR = &H10                ' (D) A folder item has been removed.
  SHCNE_MEDIAINSERTED = &H20     ' (G) Storage media has been inserted into a drive.
  SHCNE_MEDIAREMOVED = &H40      ' (G) Storage media has been removed from a drive.
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值