Search code examples
vb.netdirectorycopyfilesystemwatcher

Copy File on Modification FIleSystemWatcher, Visual Basic (VS 2012 V11)


After looking through many sites and some tutorial videos, I'm still stumped on this one. I'm finishing up a program and I need to add one last bit of functionality.

The program works this way. The user specifies a file in textbox1 and then specifies a directory in textbox2. The user sets how often they want the file to by copied in textbox3. The user hits run and the program copies the file to the new location, adding a number to the file name each time it is copied (to avoid overwrites). That all works fine, but I want the user to have the choice to either copy the file by time or when the file is modified.

How can I use the FileSystemWatcher to look for modification in the directory (given in textbox1) and then call the statement that copies the specified directory to the target destination (specified in textbox 2)?

Additional Note:

In one tutorial the FileSystemWatcher path was set up by doing this

Dim watched As String = System.IO.Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "Pictures")
        Dim fsw As New FileSystemWatcher(watched)

The path that the code directs to is "C:\Users[User Name]\Pictures" . I can't find a resource online that shows what variables ".GetEnvironmentVariable" accepts or even what the variables mean. This is one of many reasons why I am having trouble with this last bit code.


Solution

  • GetEnvironmentVariable returns the value for the specified environment for the current process.

    In the case of your example, USERPROFILE is the path to the folder for the current user. For example, on my laptop USERPROFILE is C:\Users\Tim.

    The output of System.IO.Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "Pictures") would be the USERPROFILE path plus "Pictures" - to continue with my example, it would be C:\Users\Tim\Pictures - which is the physical path to the My Pictures folder for my user account.

    To get a list of all your environment variables, if you're curious, go to the DOS prompt and type in SET and hit return.

    To answer your original question, you need to handle the FileSystemWatcher.Changed Event.

    For example:

    Private Shared Sub OnChanged(source As Object, e As RenamedEventArgs)
    
       ' Do your copy here
    End Sub
    

    You can hook the event handler up to your FileWatcher like this:

    AddHandler fsw.Changed, AddressOf OnChanged
    

    However, pay attention to this warning from the MSDN docs.

    Common file system operations might raise more than one event. For example, when a file is moved from one directory to another, several OnChanged and some OnCreated and OnDeleted events might be raised. Moving a file is a complex operation that consists of multiple simple operations, therefore raising multiple events. Likewise, some applications (for example, antivirus software) might cause additional file system events that are detected by FileSystemWatcher.