Results 1 to 6 of 6

Thread: FileSystemWatcher

  1. #1

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    FileSystemWatcher

    Hi!

    I'm using this class to watch a certain file:

    Code:
    Public Class UpdateWatcher
        Private _parentFrom As CPServer
    
        Public Property parentFrom() As CPServer
            Get
                Return _parentFrom
            End Get
            Set(ByVal value As CPServer)
                _parentFrom = value
            End Set
        End Property
    
        Public Sub New()
            StartWatching()
        End Sub
    
        Public Sub StartWatching()
            Dim Watcher As New FileSystemWatcher
            Watcher.Path = "D:\Inetpub\wwwroot\admin\Listner"
            Watcher.NotifyFilter = NotifyFilters.LastWrite
            Watcher.Filter = "WatchMe.txt"
            AddHandler Watcher.Changed, AddressOf OnChanged
            Watcher.EnableRaisingEvents = True
        End Sub
    
        Private Sub OnChanged(ByVal Source As Object, ByVal e As FileSystemEventArgs)
            parentFrom.RefreshDataBase()
        End Sub
    
    End Class
    and this VBScript code to write to the file every time someone making a change inside our backoffice

    Code:
    FUNCTION WriteToFile()
    	dim filesys, filetxt
    	Const ForReading = 1, ForWriting = 2, ForAppending = 8
    	Set filesys = CreateObject("Scripting.FileSystemObject")
    	Set filetxt = filesys.OpenTextFile("D:\Inetpub\wwwroot\admin\Listner\WatchMe.txt", ForWriting, True, -1)
    	filetxt.WriteLine(Now())
    	filetxt.Close 
    	Set filesys = nothing
    Set filetxt = nothing
    END FUNCTION
    from some reason the even is firing 2-4 times every time this file is been changed, any idea why?
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  2. #2
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

    Re: FileSystemWatcher

    I got this from MSDN:
    ----
    The Changed event is raised when changes are made to the size, system attributes, last write time, last access time, or security permissions of a file or directory in the directory being monitored.
    ----

    So not only when the content has changed.

    1. Opening the file (last access time)
    2. Saving/Closing the file (size)
    + maybe windows is updating the size of the file while you are editing it... or the user is pressing CTRL+S while typing. So that could be the reason why it's firing more then two times.

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,267

    Re: FileSystemWatcher

    Gonza is correct. It is not unusual to get more than one event per.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: FileSystemWatcher

    so why we use notifyfilter property?
    Code:
       Watcher.NotifyFilter = NotifyFilters.LastWrite
    anyway i tackle this by setting the a datetime variable and only commit the updates if more then X seconds was passed since the last one.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  5. #5
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

    Re: FileSystemWatcher

    Quote Originally Posted by motil View Post
    so why we use notifyfilter property?
    Code:
       Watcher.NotifyFilter = NotifyFilters.LastWrite
    anyway i tackle this by setting the a datetime variable and only commit the updates if more then X seconds was passed since the last one.
    Sry, didn't see that. That's weird...

    I found this:
    http://www.ryanmcdonnell.com/filesys...he-same-event/
    That guy says it could be things like a virusscanner checking the file. That makes sense...

  6. #6
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: FileSystemWatcher

    If it's a small text file, then you could modify your code like this:
    Code:
    Imports System.IO
    Imports System.Text
    Imports System.Security.Cryptography
    Public Class UpdateWatcher
        'A new member variable
        Private currMD5 As String = String.Empty()
       
        'A new method
        Private Function MD5(ByVal filePath As String) As String
            Using fS As New FileStream(filePath, FileMode.Open, FileAccess.Read)
                Using sP As New MD5CryptoServiceProvider
                    Dim hash() As Byte = sP.ComputeHash(fS)
                    Dim sB As New StringBuilder(hash.Length * 2)
                    For Each b As Byte In hash
                        sb.Append(b.ToString("X2"))
                    Next
                    Return sb.ToString().ToLower()
                End Using
            End Using
        End Function
    
        'Slightly modified event handler
        Private Sub OnChanged(ByVal Source As Object, ByVal e As FileSystemEventArgs)
            Dim fHash As String = MD5(e.FullPath)
            If fHash <> currMD5
                currMD5 = fHash
                parentFrom.RefreshDataBase()
            End If
        End Sub
    End Class
    This method will only invoke your parent form's refresh method if the file has physically changed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width