FileSystemWatcher want see if file is changed?

Hello,

I have a problem with an application I am building that should copy a bgi file (from the program BgInfo) each time the file changes (when a user has edited the bgi file).

But my application doesn't want to copy the file, when it changes.

This is the source code:

Imports System

Imports System.IO

Public Class FileCopyAgentMain

''Declare the public variables that can be used in all subs and functions

Public errortext As String = Nothing

Public PathToFileToWatch As String = "\\5.121.178.206\test\BgInfo\"

Public FileToWatch As String = "test.txt"

Public PathToCopyTo As String = "C:\BgInfo\"

''When the program starts, it copies the file and calls the sub CopyFileIfExists

Private Sub FileCopyAgentMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Try

'File.Copy(PathToFileToWatch & FileToWatch, PathToCopyTo & FileToWatch, True)

CopyFileIfExists()

Catch ex As System.Exception

''Error handler

errortext = _

"Error number: " & Err.Number & vbNewLine & _

"Error source: " & ex.StackTrace & vbNewLine & _

"Error descripton: " & ex.Message & vbNewLine

EventLog.WriteEntry("FileCopyAgent", errortext, EventLogEntryType.Error)

End Try

End Sub

Private Sub CopyFileIfExists()

Try

Dim i As New FileSystemWatcher(PathToFileToWatch, FileToWatch)

Do

''Watch the file and see if it changes

i.WaitForChanged(WatcherChangeTypes.Changed)

''When the file changes, then it will be copied to the local machine

File.Copy(PathToFileToWatch & FileToWatch, PathToCopyTo & FileToWatch, True)

Loop

Catch ex As System.Exception

''Error handler

errortext = _

"Error number: " & Err.Number & vbNewLine & _

"Error source: " & ex.StackTrace & vbNewLine & _

"Error descripton: " & ex.Message & vbNewLine

EventLog.WriteEntry("FileCopyAgent", errortext, EventLogEntryType.Error)

End Try

End Sub

End Class

I have also tried to use WatcherChangeTypes.All instead of WatcherChangeTypes.Changed but it wasn't helping.

The program works fine if I try to copy a txt file from the unc path to the local machine.



Answer this question

FileSystemWatcher want see if file is changed?

  • NomadaPT

    How many active filesystemwatcher can there be on a network
  • fahadabdulaziz

    Thank you DMan1!

    I just added:

    System.Threading.Thread.Sleep(10000)

    Before the copy command and it works fine now :)


  • Vaish

    Okay, now I just tried to copy the code you gave me, and I get an error about that it can't copy the file because it is in use by an other process

    How can I get it to wait until the process for the file is released or is there just a way to copy the file

    This is the code:

    Imports System

    Imports System.IO

    Imports System.Diagnostics

    Public Class FileCopyAgentMain

    ''Declare the public variables that can be used in all subs and functions

    ''When the program starts, it copies the file and calls the sub CopyFileIfExists

    Private Sub FileCopyAgentMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Try

    'File.Copy(PathToFileToWatch & FileToWatch, PathToCopyTo & FileToWatch, True)

    Run()

    Catch ex As System.Exception

    ''Error handler

    errortext = _

    "Error number: " & Err.Number & vbNewLine & _

    "Error source: " & ex.StackTrace & vbNewLine & _

    "Error descripton: " & ex.Message & vbNewLine

    EventLog.WriteEntry("FileCopyAgent", errortext, EventLogEntryType.Error)

    End Try

    End Sub

    <Security.Permissions.PermissionSet(Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _

    Private Shared Sub Run()

    ' Create a new FileSystemWatcher and set its properties.

    Dim watcher As New FileSystemWatcher()

    watcher.Path = PathToFileToWatch

    ' Watch for changes in LastAccess and LastWrite times, and

    ' the renaming of files or directories.

    watcher.NotifyFilter = (NotifyFilters.LastAccess Or NotifyFilters.LastWrite _

    Or NotifyFilters.FileName Or NotifyFilters.DirectoryName)

    watcher.Filter = FileToWatch

    ' Add event handlers.

    AddHandler watcher.Changed, AddressOf OnChanged

    AddHandler watcher.Created, AddressOf OnChanged

    AddHandler watcher.Deleted, AddressOf OnChanged

    AddHandler watcher.Renamed, AddressOf OnRenamed

    ' Begin watching.

    watcher.EnableRaisingEvents = True

    End Sub

    ' Define the event handlers.

    Private Shared Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)

    ' Specify what is done when a file is changed, created, or deleted.

    File.Copy(PathToFileToWatch & FileToWatch, PathToCopyTo & FileToWatch, True)

    End Sub

    Private Shared Sub OnRenamed(ByVal source As Object, ByVal e As RenamedEventArgs)

    ' Specify what is done when a file is renamed.

    File.Copy(PathToFileToWatch & FileToWatch, PathToCopyTo & FileToWatch, True)

    End Sub

    End Class


  • sic0198

    Two issues to be aware of...one is the OS and framework version of the remote machine

    the other is the number of active filesystem watchers on the same directory....

    http://msdn2.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx

    You also should be using the filesystem watchers changed event to do the copying...not your loop

    http://msdn2.microsoft.com/en-us/library/system.io.filesystemwatcher.changed.aspx



  • A.Carter

    That depends upon the OS of the machine being watched...see the link to the documentation that I provided...However as previously eluded to...I believe your problem is not using the filsystemwatcher "Changed" event to do your coping

  • ClaudiaHelpOnVSTO

    See -Scott Wisniewski, MS VB Compiler Dev answer in this thread:

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=617877&SiteID=1



  • FileSystemWatcher want see if file is changed?