Usman ur Rehman Ahmed's blog

Informant : A file system watcher utility

As the name suggests, FileSystemWatcher class from System.IO namespace is a need at times when you need to observe a change at a given file system location. The change may be file creation, file deletion, file rename, etc. I came across such a need today when I had to identify where a temperory file is created by an application during execution. So I wrote down a utility using above class. Later when I was done I thought of extending the utility so that it might help someone else,

Informant
Using the utility you can configure to monitor following,

-          Name of the file

-          Name of the directory

-          Attribute of the file or folder

-          Size of the file or folder

-          Date of modification

-          Date of access

-          Time of file or folder creation

You can log the changes with username and time of event, traverse subdirectories to be monitored, view the graphical representation of file system location being monitored and most important of all, select to play the application in system tray where it will report an event as soon as it occurs as shown in the figure,

Activity_monitored

Important snippets from code are presented below,

    Protected Sub ParseFolderStructure(ByVal folderPath As String, ByVal treeNode As TreeNode)
        Try
            Dim dirInfo As New IO.DirectoryInfo(folderPath)
            Dim fileObject As FileSystemInfo
            For Each fileObject In dirInfo.GetFileSystemInfos()
                If (fileObject.Attributes And FileAttributes.Directory) = FileAttributes.Directory Then
                    If chkIncludeSubdirectories.Checked Then
                        treeNode.Nodes.Add(fileObject.FullName, fileObject.FullName, 0)
                        ParseFolderStructure(fileObject.FullName, treeNode.Nodes.Add(fileObject.FullName))
                    End If
                Else
                    treeNode.Nodes.Add(fileObject.FullName, fileObject.FullName, 1)
                End If
            Next
        Catch
            Throw
        End Try
    End Sub

FolderWatcher object instantiation to look for a given path,

    FileSystemWatcher objFileSystemWatcher = New FileSystemWatcher()
    objFileSystemWatcher.Path = Me.txtUri.Text
    objFileSystemWatcher.EnableRaisingEvents = True

Event handlers association for various events,

    objFileSystemWatcher.IncludeSubdirectories = False
    AddHandler objFileSystemWatcher.Changed, AddressOf LogChange
    AddHandler objFileSystemWatcher.Created, AddressOf LogChange
    AddHandler objFileSystemWatcher.Deleted, AddressOf LogChange
    AddHandler objFileSystemWatcher.Renamed, AddressOf LogRename

Association of various Notify filters,

    objFileSystemWatcher.NotifyFilter = IO.NotifyFilters.FileName
    objFileSystemWatcher.NotifyFilter = IO.NotifyFilters.DirectoryName
    objFileSystemWatcher.NotifyFilter = objFileSystemWatcher.NotifyFilter Or IO.NotifyFilters.Attributes
    objFileSystemWatcher.NotifyFilter = objFileSystemWatcher.NotifyFilter Or IO.NotifyFilters.Size
    objFileSystemWatcher.NotifyFilter = objFileSystemWatcher.NotifyFilter Or IO.NotifyFilters.LastWrite
    objFileSystemWatcher.NotifyFilter = objFileSystemWatcher.NotifyFilter Or IO.NotifyFilters.LastAccess
    objFileSystemWatcher.NotifyFilter = objFileSystemWatcher.NotifyFilter Or IO.NotifyFilters.CreationTime
    objFileSystemWatcher.NotifyFilter = objFileSystemWatcher.NotifyFilter Or IO.NotifyFilters.Security

and showing a balloon tooltip upon activity for log change,

    Protected Sub LogChange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
        Me.NotifyIcon1.BalloonTipTitle = "Activity Monitored"
        Dim msg As String = "File " & e.FullPath & " was deleted"
        Me.NotifyIcon1.BalloonTipText = msg
        NotifyIcon1.ShowBalloonTip(2000)   
    End Sub

Download executable: http://usmanahmed.info/downloads/informant.zip

Download source code: http://usmanahmed.info/downloads/informant-source.zip [Visual Studio 2008 using VB .net]

11
To Posterous, Love Metalab