FileSystemWatcher show form on file create

Posted on 2006-04-02
Medium Priority
Last Modified: 2010-04-23
Hey Experts,

I have been smashing my head into a wall the last week trying to figure this out. I am sure it will turn out being something simple.

First off, I am using VB.NET from VS 2005.

I am monitoring a directory for a specific file type, lets say they are txt files.
On creation I want to create an instance of form2.
set vars on it, then show it.

so I have the following which fires  when the watcher detects a change
Private Sub logchange(ByVal source As Object, ByVal e As _
        If e.ChangeType = IO.WatcherChangeTypes.Changed Then

            '  MsgBox("File " & e.FullPath & _
            '                        " has been modified" & vbCrLf)
        End If
        If e.ChangeType = IO.WatcherChangeTypes.Created Then

            ' MsgBox("File " & e.FullPath & _
            '                        " has been created" & vbCrLf)
            Dim myFrom as new Form2
            myForm.label1.text="SOME VALUE"

        End If
        If e.ChangeType = IO.WatcherChangeTypes.Deleted Then

            'MsgBox("File " & e.FullPath & _
            '                         " has been deleted" & vbCrLf)
        End If
    End Sub

This works great when I just use message boxes but when attempt to display the form, it appears then kinda locks in place creating a white backround.

I understand that the watcher is running on another thread, and understand the concept of invoke and delegates but I simply can not figure this out.
Question by:kevin1265
LVL 10

Assisted Solution

bchoor earned 100 total points
ID: 16357253
I'm not sure if you copied-pasted directly from your code. You dimension 'myFrom' but are setting on 'myForm'
Dim myFrom as new Form2


You may try also - myForm.ShowDialog() - so then it'll be modal - like a message box.


LVL 10

Accepted Solution

ericwong27 earned 1900 total points
ID: 16357303
Try this

I using CLR2.0 new feature (No multi form pop up)

Form2.Text = "Create"

Instead of

Dim myform as new form2
myform.Text = "Create"

Public Class Form1

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

        With Me.FileSystemWatcher1
            .EnableRaisingEvents = True
            .Path = "C:\Temp"
            .Filter = "*.*"
        End With

    End Sub

    Private Sub FileSystemWatcher1_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) _
    Handles FileSystemWatcher1.Changed, FileSystemWatcher1.Created, FileSystemWatcher1.Deleted

        Select Case e.ChangeType
            Case IO.WatcherChangeTypes.Created
                Form2.Text = "Create"
            Case IO.WatcherChangeTypes.Deleted
                Form2.Text = "Delete"
            Case IO.WatcherChangeTypes.Renamed
                Form2.Text = "Rename"
        End Select

    End Sub

End Class

Author Comment

ID: 16357546
Eric you the man, you have no idea how much this helped.

Bchoor thanks this works as well however it freezes my GUI.

Thanks for the quick response.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question