Results 1 to 4 of 4

Thread: [RESOLVED] FileSystemWatcher not detecting change in file

  1. #1

    Thread Starter
    Addicted Member adamlonsdale's Avatar
    Join Date
    Oct 2005
    Posts
    210

    Resolved [RESOLVED] FileSystemWatcher not detecting change in file

    Hey there,

    I've written the following code. It works fine for creating, deleting and renaming files. However when I set a file to read only, or modify the file (i.e open it in notepad and change the contents) nothing happens (it doesn't hit the change breakpoint)

    I have the following code:

    VB.NET Code:
    1. Private fsw As FileSystemWatcher
    2.  
    3.  Public Sub WatchFiles(ByVal tblMappings As DataSet)
    4.         For Each row As DataRow In tblMappings.Tables(0).Rows
    5.             Try
    6.                 fsw = New FileSystemWatcher
    7.                 Dim FolderPath As String = row.Item(2)
    8.                 If Directory.Exists(FolderPath) Then
    9.  
    10.                     fsw.Path = FolderPath
    11.                     fsw.NotifyFilter = IO.NotifyFilters.DirectoryName
    12.                     fsw.NotifyFilter = fsw.NotifyFilter Or IO.NotifyFilters.FileName
    13.                     fsw.NotifyFilter = fsw.NotifyFilter Or IO.NotifyFilters.Attributes
    14.                     fsw.NotifyFilter = fsw.NotifyFilter Or IO.NotifyFilters.LastWrite
    15.                     fsw.NotifyFilter = fsw.NotifyFilter Or IO.NotifyFilters.Size
    16.                     fsw.NotifyFilter = fsw.NotifyFilter Or IO.NotifyFilters.LastAccess
    17.                     fsw.NotifyFilter = NotifyFilters.LastAccess Or NotifyFilters.DirectoryName Or NotifyFilters.FileName Or NotifyFilters.CreationTime
    18.                     fsw.IncludeSubdirectories = False
    19.                     fsw.EnableRaisingEvents = True
    20.  
    21.                     AddHandler fsw.Changed, AddressOf fsw_changed
    22.                     AddHandler fsw.Created, AddressOf fsw_created
    23.                     AddHandler fsw.Renamed, AddressOf fsw_renamed
    24.                     AddHandler fsw.Deleted, AddressOf fsw_deleted
    25.                     fsw.EnableRaisingEvents = True
    26.                 End If
    27.  
    28.             Catch ex As Exception
    29.                 MessageBox.Show(ex.Message)
    30.             End Try
    31.         Next
    32.  
    33.     End Sub
    34.    
    35.  Private Sub fsw_changed(ByVal sender As Object, ByVal e As FileSystemEventArgs)
    36.         Dim db As New DatabaseFunctions
    37.         db.AddToLog(DatabaseFunctions.PendingType.Change, e.FullPath, GetServerFolderFromPCFolder(ftp.GetFolder(e.FullPath), _tblMappings), String.Empty, _strUsername, _strPassword, _strDomain, _strAusername, _tblMappings)
    38.     End Sub

    Does anybody have any ideas as to whats going wrong?

    Regards,

    Adam.

  2. #2

    Thread Starter
    Addicted Member adamlonsdale's Avatar
    Join Date
    Oct 2005
    Posts
    210

    Re: FileSystemWatcher not detecting change in file

    Hmm so it seems to hit this if I move a file (moving a file does change - delete - delete - create). So it's not a question of the actual plumbing of the code. I think it is something to do with the notifyfilter.

    Adam.

  3. #3
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: FileSystemWatcher not detecting change in file

    Are you refilling the dataset?
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  4. #4

    Thread Starter
    Addicted Member adamlonsdale's Avatar
    Join Date
    Oct 2005
    Posts
    210

    Re: FileSystemWatcher not detecting change in file

    Hey!

    Have just discovered that the issue is with this line:
    fsw.NotifyFilter = NotifyFilters.LastAccess Or NotifyFilters.DirectoryName Or NotifyFilters.FileName Or NotifyFilters.CreationTime

    Instead of using &= I've used =. Which means that all the filters (i.e size and lastwrite) that i set before were scrapped and replaced with these! Silly mistake I know!

    Thanks

    Adam.

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