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
SystemImports
System.IOPublic
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 handlererrortext = _
"Error number: " & Err.Number & vbNewLine & _ "Error source: " & ex.StackTrace & vbNewLine & _ "Error descripton: " & ex.Message & vbNewLineEventLog.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 changesi.WaitForChanged(WatcherChangeTypes.Changed)
''When the file changes, then it will be copied to the local machineFile.Copy(PathToFileToWatch & FileToWatch, PathToCopyTo & FileToWatch,
True) Loop Catch ex As System.Exception ''Error handlererrortext = _
"Error number: " & Err.Number & vbNewLine & _ "Error source: " & ex.StackTrace & vbNewLine & _ "Error descripton: " & ex.Message & vbNewLineEventLog.WriteEntry(
"FileCopyAgent", errortext, EventLogEntryType.Error) End Try End SubEnd
ClassI 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.

FileSystemWatcher want see if file is changed?
NomadaPT
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
SystemImports
System.IOImports
System.DiagnosticsPublic
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 handlererrortext = _
"Error number: " & Err.Number & vbNewLine & _ "Error source: " & ex.StackTrace & vbNewLine & _ "Error descripton: " & ex.Message & vbNewLineEventLog.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 SubEnd
Classsic0198
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
ClaudiaHelpOnVSTO
See -Scott Wisniewski, MS VB Compiler Dev answer in this thread:
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=617877&SiteID=1