Question Copy folder contents upon change

Caleb

New member
Joined
May 20, 2010
Messages
1
Programming Experience
Beginner
I'm trying to create a simple program that will copy the contents of one folder to another folder as soon as they change in the first folder, whether files are added, deleted, or modified. I'm currently just trying to get this example working without crashing.

I have a small window with a text box input for the "watch folder" path, a start watch button, a stop watch button, and a text box for the output.

Here's the code...

VB.NET:
Imports System.IO
Imports System.Diagnostics

Public Class FolderMirror

    Private watchfolder As FileSystemWatcher

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

    End Sub

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

    End Sub

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_mirrorpath.TextChanged

    End Sub

    Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click

    End Sub

    Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click

    End Sub

    Private Sub btn_start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_start.Click
        watchfolder = New System.IO.FileSystemWatcher()
        watchfolder.Path = txt_watchpath.Text

        watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
        watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.FileName
        watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.Attributes

        AddHandler watchfolder.Changed, AddressOf logchange
        AddHandler watchfolder.Created, AddressOf logchange
        AddHandler watchfolder.Deleted, AddressOf logchange

        AddHandler watchfolder.Renamed, AddressOf logrename

        watchfolder.EnableRaisingEvents = True

        btn_start.Enabled = False
        btn_stop.Enabled = True

    End Sub

    Private Sub logchange(ByVal source As Object, ByVal e As  _
                       System.IO.FileSystemEventArgs)
        If e.ChangeType = IO.WatcherChangeTypes.Changed Then
            txt_folderactivity.Text &= "File " & e.FullPath & _
                                   " has been modified" & vbCrLf
        End If
        If e.ChangeType = IO.WatcherChangeTypes.Created Then
            txt_folderactivity.Text &= "File " & e.FullPath & _
                                      " has been created" & vbCrLf
        End If
        If e.ChangeType = IO.WatcherChangeTypes.Deleted Then
            txt_folderactivity.Text &= "File " & e.FullPath & _
                                    " has been deleted" & vbCrLf
        End If
    End Sub

    Public Sub logrename(ByVal source As Object, ByVal e As  _
                           System.IO.RenamedEventArgs)
        txt_folderactivity.Text &= "File" & e.OldName & _
                             " has been renamed to " & e.Name & vbCrLf
    End Sub

    Private Sub btn_stop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_stop.Click
        watchfolder.EnableRaisingEvents = False
        btn_start.Enabled = True
        btn_stop.Enabled = False
    End Sub

End Class

The program will run, I input the watch folder path, click Start Watch. I go to the folder and change a file, but the program crashes and the Immediate Window displays...
A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll

I've been searching high and low and haven't found a solution. I'm sure it's something simple, but I'm not extremely familiar with vb.net

Thanks in advance.
 
Back
Top