Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Monitoring folder activities

Total Hit ( 3072)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


Click here to download the attached file  


In this article I will show you how to use FindFirstChangeNotification, FindNextChangeNotification, FindCloseChangeNotification and WaitForSingleObject API. Unfortunately these all APIs can only give you indication of event but they do not tell you which file/folder has been changed and what event occured. You can use SHChangeNotifyRegister and SHChangeNotifyDeregister to get filename and event type using subclassing technique.

Here is the few basic steps to implement folder monitoring.

1. Call FindFirstChangeNotification with one or more following options

2. If FindFirstChangeNotification is successful then you can call WaitForSingleObject to recive any
mathing event notification. WaitForSingleObject returns WAIT_OBJECT_0 if any mathing event occurs.
3. After WaitForSingleObject you can call FindNextChangeNotification to continue to recive more notification.
4. If you don't want to monitor then you can call FindCloseChangeNotification which will stop tracking any further events.

Step-By-Step Example
- Create a standard exe project
- Add two commandbutton controls, one checkbox control on the form1
- Add one timer control, one directory control and one drive control on the form1

Click here to copy the following block
Option Explicit

Private Declare Function FindFirstChangeNotification Lib "kernel32" Alias "FindFirstChangeNotificationA" ( _
    ByVal lpPathName As String, _
    ByVal bWatchSubtree As Long, _
    ByVal dwNotifyFilter As Long) As Long

Private Declare Function FindNextChangeNotification Lib "kernel32" ( _
    ByVal hChangeHandle As Long) As Long

Private Declare Function FindCloseChangeNotification Lib "kernel32" ( _
    ByVal hChangeHandle As Long) As Long

Private Declare Function WaitForSingleObject Lib "kernel32" ( _
    ByVal hHandle As Long, _
    ByVal dwMilliseconds As Long) As Long

Const FILE_NOTIFY_CHANGE_ATTRIBUTES = &H4
Const FILE_NOTIFY_CHANGE_DIR_NAME = &H2
Const FILE_NOTIFY_CHANGE_FILE_NAME = &H1
Const FILE_NOTIFY_CHANGE_LAST_WRITE = &H10
Const FILE_NOTIFY_CHANGE_SECURITY = &H100
Const FILE_NOTIFY_CHANGE_SIZE = &H8

Const INVALID_HANDLE_VALUE = -1
Const WAIT_OBJECT_0 = 0

Dim hFile As Long
Dim WatchPath As String
Dim Flag As Boolean, Started As Boolean

Private Sub Command1_Click()
  WatchPath = Dir1.Path & Chr$(0)
  Timer1.Enabled = True
End Sub

Private Sub Command2_Click()
  If hFile <> 0 Then Call FindCloseChangeNotification(hFile)
  Timer1.Enabled = False
  Command1.Enabled = True
  Command2.Enabled = False
End Sub

Private Sub Drive1_Change()
  Dir1.Path = Drive1.Drive
End Sub

Private Sub Form_Load()
  Timer1.Enabled = False
  Command2.Enabled = False
  Check1.Value = 1
  Dir1.Path = "c:\"
  Check1.Caption = "Monitor Sub Direcories"
  Command1.Caption = "Start Monitoring"
  Command2.Caption = "Stop Monitoring"
End Sub

Private Sub Timer1_Timer()
  If Not Started Then

    hFile = FindFirstChangeNotification(WatchPath, CBool(Check1.Value), _
        FILE_NOTIFY_CHANGE_FILE_NAME Or _
        FILE_NOTIFY_CHANGE_ATTRIBUTES Or _
        FILE_NOTIFY_CHANGE_DIR_NAME Or _
        FILE_NOTIFY_CHANGE_LAST_WRITE Or _
        FILE_NOTIFY_CHANGE_SIZE)

    If hFile <> INVALID_HANDLE_VALUE Then
      Started = True
      Command1.Enabled = False
      Command2.Enabled = True
    Else
      Command1.Enabled = True
      Command2.Enabled = False
    End If
  Else
    If WaitForSingleObject(hFile, 50) = WAIT_OBJECT_0 Then
      Form1.List1.AddItem "Event occured : " & Now
      Beep
    End If

    Call FindNextChangeNotification(hFile)

    If hFile = 0 Then
      '//If handle is zero then stop monitoring
      Call FindCloseChangeNotification(hFile)
      Started = False
      Timer1.Enabled = False
      Command1.Enabled = True
      Command2.Enabled = False
    End If
  End If
End Sub


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.