I am using FileSystemWatcher to copy the contents of a text file after it has been updated but it is creating the same avent more than once i was wondering if anyone could tell me if there is a way to stop it doing that so it will only copy the contents once.

Here is my code any help would be great.


Dim FileGoto As String

FileGoto = "c:\watch\"

If e.ChangeType = IO.WatcherChangeTypes.Changed Then

'To Copy File
'System.IO.File.Copy(e.FullPath, FileGoto & e.Name)

Dim TextLine As String = ""

Dim objReader As New System.IO.StreamReader(e.FullPath)

Do While objReader.Peek() <> -1
TextLine = TextLine & objReader.ReadLine() & vbNewLine
Loop

TextBox1.Text = TextLine

Dim objWriter As New System.IO.StreamWriter(FileGoto & e.Name, True)
objWriter.Write(TextBox1.Text)
objWriter.Close()

End If


Many thanks

IO.WatcherChangeTypes.Changed is raised when the file's or the folder's size, attributes, last accessed etc. are changed.

First, filter FileSystemWatcher1.NotifyFilter = NotifyFilters.LastWrite This should assure that the event is raised only when the file is updated. However, you still get the same event twice because the folder is updated too.

Use a flag to trap the second event

Private SecondEvent As Boolean = False

Private Sub FileSystemWatcher1_Changed(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed
  Dim FileGoto As String

  FileGoto = "D:\"

  If e.ChangeType = IO.WatcherChangeTypes.Changed AndAlso Not SecondEvent Then

    'To Copy File
    'System.IO.File.Copy(e.FullPath, FileGoto & e.Name)

    Dim TextLine As String = ""

    Dim objReader As New System.IO.StreamReader(e.FullPath)

    Do While objReader.Peek() <> -1
      TextLine = TextLine & objReader.ReadLine() & vbNewLine
    Loop

    TextBox1.Text = TextLine

    Dim objWriter As New System.IO.StreamWriter(FileGoto & e.Name, True)
    objWriter.Write(TextBox1.Text)
    objWriter.Close()
    ' Set flag for file change
    SecondEvent = True
  Else
    ' Trap the 'folder' change i.e. reset the flag
    SecondEvent = False
  End If
End Sub

Now the second event simply resets the flag.

That is great it worked a treat no more duplicates thanks.

But i am going to be cheaky and ask another question about it. that code pulls the text out of a newly updated file and copys it to another but it wont let the first file be updated more than once it seams to be looking it. Any ideas? maybe in not closeing the read or write in the right place but im not too sure.

Thanks Again

Will

Instead of System.IO namespace, use My namespace (VB.2005 and newer). You can copy a file with only a few lines of code:

If My.Computer.FileSystem.FileExists(e.FullPath) Then
  Dim AllText As String
  AllText = My.Computer.FileSystem.ReadAllText(e.FullPath)
  My.Computer.FileSystem.WriteAllText(FileGoto & e.Name, AllText, False)
End If
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.