troubleshooting Question

VB.net Filesystem watcher not working

Avatar of Kevin
KevinFlag for United States of America asked on
.NET ProgrammingVisual Basic.NET
5 Comments1 Solution471 ViewsLast Modified:
Good Afternoon,

I can't seem to get my FileSystemWatcher to work. I want it to display the results of the watch folder in a list box. But when I copy a file to the watched directory no data is written to the list box control. Please note that I am not getting any errors.

Kindly advise if I am missing something.

Code is below.

Public Class frmConfigurations

    'Connection Variables
    Dim conn As SqlConnection
    Dim connectionString = SQLConnString()
    Dim sql As String

    'WatchFolder Variables
    Public watchfolder As FileSystemWatcher
    Public str As String = ""

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

        Try

            conn = New SqlConnection(connectionString)
            conn.Open()

            sql = "SELECT ID, ClientNumber, OfficerName, OfficerEmail, AssistantName, AssistantEmail FROM Contacts ORDER BY ClientNumber"

            Dim dscmd As New SqlDataAdapter(sql, conn)
            dscmd.SelectCommand.CommandTimeout = 60

            Dim ds As New DataSet
            dscmd.Fill(ds)

            dgvData.DataSource = ds.Tables(0)

        Catch ex As Exception
            MsgBox(ErrorToString)

        Finally
            conn.Close()

        End Try

        'Set the Monitoring Directory
        txtWatchPath.Text = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\GlobusMailWatch"

        'Create the monitoring directory if it does not exist already
        If Not Directory.Exists(txtWatchPath.Text) Then
            Directory.CreateDirectory(txtWatchPath.Text)
        End If   

Private Sub btnStart_Click(sender As System.Object, e As System.EventArgs) Handles btnStart.Click

        btnStart.Enabled = False
        btnStop.Enabled = True
        txtMonStatus.Text = "Started"

        'Prepare to handle the monitoring folder
        subInitializeWatchFolder()

    End Sub

Private Sub subInitializeWatchFolder()

        watchfolder = New FileSystemWatcher

        'This is the path we are monitoing
        watchfolder.Path = txtWatchPath.Text

        'Watch for changes
        watchfolder.NotifyFilter = NotifyFilters.CreationTime

        'Only watch for .MSG files
        watchfolder.Filter = "*.msg"

        'Event Handlers
        AddHandler watchfolder.Created, AddressOf logCreated
        AddHandler watchfolder.Deleted, AddressOf logDeleted

        'Start monitoring folder
        watchfolder.EnableRaisingEvents = True

    End Sub

Private Sub logCreated(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)

        str = e.FullPath

        lsbxLog.Items.Add("CREATED: " & str)

    End Sub

    Private Sub logDeleted(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)

        str = e.FullPath

        lsbxLog.Items.Add("DELETED: " & str)

    End Sub
ASKER CERTIFIED SOLUTION
it_saige
Developer
Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros