Problem with file system watcher app

Blake81

Well-known member
Joined
Feb 23, 2006
Messages
304
Location
Georgia, USA
Programming Experience
1-3
This program is supposed to watch a folder and display Form2 (with the text of Label1 in Form2 equal to specific text) when a file contained in a String Collection is added to the folder. It's giving me two problems. One is that it seems to freeze up when Form2 shows, and the other is that the text of Label1 on Form2 is never equal to anything. I'd appreciate any help with this.

VB.NET:
Public Class Form1
    WithEvents fsw As System.IO.FileSystemWatcher
    

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim x As Integer = 0
        For x = 0 To My.Settings.Orders.Count - 1
            If My.Settings.Orders(x) = TextBox2.Text Then
                TextBox2.Text = My.Settings.messages(x)
            End If
        Next
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        My.Settings.Orders.Add(TextBox4.Text)
        My.Settings.messages.Add(TextBox1.Text)
    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        FolderBrowserDialog1.ShowDialog()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim dm As Integer = My.Settings.Orders.IndexOf(TextBox2.Text)
        My.Settings.Orders.RemoveAt(dm)
        My.Settings.messages.RemoveAt(dm)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        My.Settings.prooffolder = FolderBrowserDialog1.SelectedPath
        Label1.Text = FolderBrowserDialog1.SelectedPath
        fsw = New System.IO.FileSystemWatcher(My.Settings.prooffolder, "*.txt")
        fsw.NotifyFilter = IO.NotifyFilters.FileName
        fsw.EnableRaisingEvents = True
    End Sub

    Private Sub fsw_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles fsw.Created
        
        Dim filecomp() As String = e.Name.Split("-")

        If My.Settings.Orders.IndexOf(filecomp(0)) >= 0 Then
            MessageBox.Show(e.Name)
            Dim myindex As Integer = My.Settings.Orders.IndexOf(filecomp(0))
            My.Settings.message = My.Settings.messages(myindex)
            Dim fm2 As Form2
            fm2 = New Form2
            fm2.TextBox1.Text = My.Settings.message
            fm2.Show()
            My.Settings.messages.RemoveAt(My.Settings.Orders.IndexOf(filecomp(0)))
            My.Settings.Orders.Remove(filecomp(0))
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not My.Settings.prooffolder = "" Then
            fsw = New System.IO.FileSystemWatcher(My.Settings.prooffolder, "*.txt")
            fsw.NotifyFilter = IO.NotifyFilters.FileName
            fsw.EnableRaisingEvents = True
        End If

    End Sub
End Class
 
Hm...A few things.

1.) What part of Georgia are you from? :)

2.) Are you getting an exception when the form freezes?

3.) How is your FolderBrowserDialog even showing? Your code for your Button2 (Change your control's names! You're displaying awful naming conventions!) should look like this. (It may not be perfect as I am typing this in the Quick Reply and not Visual Studio. You'll get the jist of it though.)

VB.NET:
Select Case Me.FolderBrowserDialog1.ShowDialog()
Case Windows.Forms.DialogResult.OK
        My.Settings.prooffolder = FolderBrowserDialog1.SelectedPath
        Label1.Text = FolderBrowserDialog1.SelectedPath
        fsw = New System.IO.FileSystemWatcher(My.Settings.prooffolder, "*.txt")
        fsw.NotifyFilter = IO.NotifyFilters.FileName
        fsw.EnableRaisingEvents = True
Case Windows.Forms.DialogResult.Cancel
Exit Sub
End Select
 
Hey, thanks for the reply.

1. Carrollton, GA :)

2. No exception, Form2 just loads and then the whole form turns white like an app does when it freezes.

3. The folder browser dialog is displayed based on a button click:
VB.NET:
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        FolderBrowserDialog1.ShowDialog()
    End Sub

I understand what your code is doing, but that step doesn't seem to be the problem. The dialog shows just fine, and it lets you select a folder to watch just fine. It even triggers when a file is created in that folder. I could actually almost do away with Form2, except that this is eventually going to be a Windows service. I'm writing it as a Form just so I can test it out to get it working. Anyway, it seems that I can't use Messagebox.Show() in a Windows service, and that's basically all I need. So, Form2 is just a big, awkward MessageBox.
 
I got this working. It may have had a few problems, but one thing I did was to use fm2.ShowDialog() instead of just Show(). Anyway, this program is fully functional now and just needs to be made a Windows service instead of a forms application. It lets you look up orders, change the note associated with an order, and delete notes. It monitors for file creation, change, and being moved/deleted.
VB.NET:
Imports WindowsApplication1.Form2
Public Class Form1
    WithEvents fsw As System.IO.FileSystemWatcher
    Public Shared filecomp() As String

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim x As Integer = 0
        For x = 0 To My.Settings.Orders.Count - 1
            If My.Settings.Orders(x) = TextBox2.Text Then
                TextBox3.Text = My.Settings.messages(x)
            End If
        Next
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        My.Settings.Orders.Add(TextBox4.Text)
        My.Settings.messages.Add(TextBox1.Text)
        TextBox4.Text = ""
        TextBox1.Text = ""
    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        FolderBrowserDialog1.ShowDialog()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim dm As Integer = My.Settings.Orders.IndexOf(TextBox2.Text)
        My.Settings.Orders.RemoveAt(dm)
        My.Settings.messages.RemoveAt(dm)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        My.Settings.prooffolder = FolderBrowserDialog1.SelectedPath
        Label1.Text = FolderBrowserDialog1.SelectedPath
        fsw = New System.IO.FileSystemWatcher(My.Settings.prooffolder, "*.txt")
        fsw.NotifyFilter = IO.NotifyFilters.FileName Or IO.NotifyFilters.LastWrite Or IO.NotifyFilters.Size
        fsw.EnableRaisingEvents = True
    End Sub

    Private Sub fsw_Changed(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles fsw.Changed
        MessageBox.Show(e.ChangeType)
        fsw_Created(sender, e)
    End Sub

    Private Sub fsw_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles fsw.Created

        filecomp = e.Name.Split("-")

        If My.Settings.Orders.IndexOf(filecomp(0)) >= 0 Then
            Dim myindex As Integer = My.Settings.Orders.IndexOf(filecomp(0))
            My.Settings.message = My.Settings.messages(myindex)
            Dim fm2 As Form2
            fm2 = New Form2
            fm2.TextBox1.Text = My.Settings.message
            fm2.ShowDialog()
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not My.Settings.prooffolder = "" Then
            fsw = New System.IO.FileSystemWatcher(My.Settings.prooffolder, "*.txt")
            fsw.NotifyFilter = IO.NotifyFilters.FileName Or IO.NotifyFilters.LastWrite Or IO.NotifyFilters.Size
            fsw.EnableRaisingEvents = True
        Else
            My.Settings.prooffolder = "C:\Documents and Settings\Owner\Desktop\New Folder"
            fsw = New System.IO.FileSystemWatcher(My.Settings.prooffolder, "*.txt")
            fsw.NotifyFilter = IO.NotifyFilters.FileName
            fsw.EnableRaisingEvents = True
        End If

    End Sub

    Private Sub fsw_Deleted(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles fsw.Deleted
        Dim myfile As String = e.Name
        Dim mysplit() As String = myfile.Split("-")
        If Not My.Settings.Orders.IndexOf(mysplit(0)) = -1 Then
            Dim rm As Integer = My.Settings.Orders.IndexOf(filecomp(0))
            My.Settings.messages.RemoveAt(rm)
            My.Settings.Orders.Remove(filecomp(0))
        End If
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim dm As Integer = My.Settings.Orders.IndexOf(TextBox2.Text)
        My.Settings.messages(dm) = TextBox3.Text
    End Sub
End Class
 
Back
Top