Problem monitoring directory for file activity in VB.net 2010...

Posted by Mike Cialowicz on Stack Overflow See other posts from Stack Overflow or by Mike Cialowicz
Published on 2011-01-17T23:31:00Z Indexed on 2011/01/17 23:53 UTC
Read the original article Hit count: 154

I'm trying to write a simple program to monitor a folder for new files in VB.NET 2010, and am having some trouble.

Here's a simplified version of what my program looks like:

Imports System.IO

Public Class Main
    Public fileWatcher As FileSystemWatcher

    Sub btnGo_Click(sender As System.Object, e As System.EventArgs) Handles btnGo.Click
        '//# initialize my FileSystemWatcher to monitor a particular directory for new files
        fileWatcher = New FileSystemWatcher()
        fileWatcher.Path = thisIsAValidPath.ToString()
        fileWatcher.NotifyFilter = NotifyFilters.FileName
        AddHandler fileWatcher.Created, AddressOf fileCreated
        fileWatcher.EnableRaisingEvents = True
    End Sub

    Private Sub fileCreated(sender As Object, e As FileSystemEventArgs)
        '//# program does not exit when I comment the line below out
        txtLatestAddedFilePath.Text = e.FullPath
        '//# e.FullPath is valid when I set a breakpoint here, but when I step into the next line, the program abruptly halts with no error code that I can see
    End Sub
End Class

As you can see, I have a button which will initialize a FileSystemWatcher when clicked. The initialization works, and when I place a new file in the monitored directory, the program reaches the fileCreated sub. I can even see that e.FullPath is set correctly. However, it exits abruptly right after that with no error code (none that I can see, anyways). If I comment everything in the fileCreated sub out, the program continues running as expected.

Any ideas as to why it's dying on me? Any help would be greatly appreciated. I'm fairly new to VS/VB.NET, so maybe I'm just making a silly mistake. Thanks!

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about visual-studio-2010