EnableRaisingEvents Help Please

Jimmy22

Member
Joined
Mar 27, 2008
Messages
15
Programming Experience
Beginner
Hi Guys

I have been trying to get this working all day..... :(:(:(:( I am write a program using the File System Watcher and this line of code

watchfolder.EnableRaisingEvents = True

keeps erroring that the path is not of a legal form but from my understanding you need the the RasingEvents set to True for it to work. As if i commet the code it goes past that point but wont watch the drive.

Also how would you use multiple file watchers say if you have two hard drives the C:\ and D:\.

Many thanks inadvance

Jimmy22
 
First you'll need to give a little bit of code so we can help you out here.

What events do you want to monitor? Are you looking for a specific file type? Should the watcher just look at the root of the drive or include subfolders?

You're going to need to supply some information before getting valid results. Since I don't know what you're looking for here's a bad example.

VB.NET:
        FileSystemWatcher1.Filter = "*.*"
        FileSystemWatcher1.Path = "C:\"
        FileSystemWatcher1.IncludeSubdirectories = False
        FileSystemWatcher1.NotifyFilter = IO.NotifyFilters.LastWrite Or IO.NotifyFilters.CreationTime
        FileSystemWatcher1.EnableRaisingEvents = True

A fsw will only watch 1 folder structure at a time (and 1 file type) so you'd need to have a seperate instance set up for each drive.
 
EnableRasingEvents

Thanks Matt for the help no luck tho its annyoing me now tho but carry on

What i am trying to do is this.

1) Watch the C:\ drive and other drives
2) Send a email to a certain user about a file has been created and the user able to edit the details could be STMP/POP3
3) raise event in event log app or system about the creation deletion ect
4) add exceptions not to be scanned like ect system32 windows and other folders

here is my code for the btnStart and btnStop of the File system watcher

VB.NET:
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        watchfolder = New System.IO.FileSystemWatcher()

        'this is the path we want to monitor
        watchfolder.Path = txtfol1.Text
        watchfolder.Filter = "*.*"
        watchfolder.IncludeSubdirectories = True

        'Add a list of Filters we want to specify, making sure
        'you use OR for each Filter as we need to all of those
        'Attributes  	The attributes of the file or folder
        'CreationTime 	The time the file or folder was created
        'DirectoryName 	The name of the directory
        'FileName 	The name of the file
        'LastAccess 	The date the file or folder was last opened
        'LastWrite 	The date the file or folder last had anything written to it
        'Security 	The security settings of the file or folder
        'Size 	The size of the file or folder

        watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
        watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
        IO.NotifyFilters.FileName

        watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
        NotifyFilters.Attributes

        watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
        NotifyFilters.CreationTime

        watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
        NotifyFilters.Size

        ' add the handler to each event
        AddHandler watchfolder.Changed, AddressOf logchange
        AddHandler watchfolder.Created, AddressOf logchange
        AddHandler watchfolder.Deleted, AddressOf logchange

        ' add the rename handler as the signature is different AddHandler watchfolder.Renamed, AddressOf logrename

        'Set this property to true to start watching
      
        watchfolder.EnableRaisingEvents = True
        btnStart.Enabled = False
        btnStop.Enabled = True

    End Sub

    Private Sub logchange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
        If e.ChangeType = IO.WatcherChangeTypes.Changed Then txtfol1.Text &= "File " & e.FullPath & _
        "has been modified" & vbCrLf


        If e.ChangeType = IO.WatcherChangeTypes.Created Then txtfol1.Text &= "File " & e.FullPath & _
        "has been created" & vbCrLf


        If e.ChangeType = IO.WatcherChangeTypes.Deleted Then txtfol1.Text &= "File " & e.FullPath & _
        "has been deleted" & vbCrLf

    End Sub

    'This is the code for handling the Renamed event raised by the FileSystemWatcher class:

    Public Sub logrename(ByVal source As Object, ByVal e As System.IO.RenamedEventArgs)
        txtfol1.Text &= "File" & e.OldName & " has been renamed to " & e.Name & vbCrLf
    End Sub

    Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
        ' Stop watching the folder
        watchfolder.EnableRaisingEvents = False
        watchfolder.IncludeSubdirectories = False
        btnStart.Enabled = True
        btnStop.Enabled = False
   End Sub

Thanks for your help guys in this :p:p:p:p:p:p:p:p
 
Are you 100% sure that txtfol1.Text is a valid folder name?

If you replace txtfol1.Text with "C:\" does it work?

I would add some code to make sure it exist. Something like

If System.IO.Directory.Exists(txtfol1.Text) Then
Do your thing

Else
MessageBox something
End IF
 
WHAT am i missing :mad: :mad: :mad:

Thanks Rocksteady and MattP for the help the problem seems to be watching the C:\ drive i can watch a directory for example "c:\Test" but not the root. I have been debugging my code.

using Rocksteadys code:

If System.IO.Directory.Exists(txtfol1.Text) Then
MsgBox("The Directory = " & " " & txtfol1.Text, MsgBoxStyle.Information, "It Exists")
End If


which dispalys the message box which means the directory exsits and also futher down just before the
the enable event raising kicks in:

MsgBox("THE FOLDER =" & " " & txtfol1.Text, MsgBoxStyle.Information, "THE OUTCOME")

Just to make sure the the txtfol1 keeps the sting which it does.

i am also looking into the DirectoryNotFoundException which is part of the error.

All help very much appricated

Cheers

Jimmy22
 
Are you 100% percent sure there are no spaces in front of C:\? ie " C:\"
Maybe the textbox has a space in it?

Replace all your txtfol1.Text with txtfol1.Text.Trim
 
ran the trim function on all txtfol1.text but still the same error do want me to post all my code.

Cheers Jimmy22
 
Here you go
Thanks for all your help
 

Attachments

  • source.zip
    1.8 KB · Views: 16
Last edited by a moderator:
Change "Public watchfolder ..." to "Public WithEvents watchfolder ...". That way, when you're assigning this variable a new instance (like you do in Form Load event, but don't have to ;)) you don't have to add all it's event handlers with AddHandler statement, but instead these are dynamically redirected for your current event handlers (Handles clause) of that variable.
 
Hi JohnH

Thanks for the reply but it didnt work :mad::mad::mad::mad:

Inherits System.Windows.Forms.Form
Public WithEvents watchfolder As New FileSystemWatcher()

Public Sub New()
Me.InitializeComponent()
End Sub

Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
'Hides prog on minimzes
Me.trayIcon.Visible = (Me.WindowState = FormWindowState.Minimized)
If Me.WindowState = FormWindowState.Minimized = True Then
Me.Hide()
End If
MyBase.OnResize(e)
End Sub

just to make sure this is the where i need to put it :rolleyes::rolleyes::rolleyes:

Thanks
 
Back
Top