Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB.NET
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
VB.NET General VB.NET discussions for issues that don't fall into other VB.NET forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old December 27th, 2003, 09:31 PM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default File System Watcher

Hello Everyone and thanks for your help in advance. I am developing an application that utilizes the FileSystemWatcher. However, I am having difficulty getting the application to determine the difference between a created file and a changed file. Here is my code:

        Dim myWatcher As New System.IO.FileSystemWatcher

        Dim myPath As String = "C:\My Documents"

        myWatcher.Path = myPath

        myWatcher.EnableRaisingEvents = True

        AddHandler myWatcher.Created, AddressOf logchange

        AddHandler myWatcher.Changed, AddressOf logchange

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

        If e.ChangeType = WatcherChangeTypes.Created Then
            Label1.Text = e.FullPath & " was created"
        ElseIf e.ChangeType = WatcherChangeTypes.Changed Then
            Label1.Text = e.FullPath & " was changed"
        End If

    End Sub

When I test this, all new files created come up as "changed" rather than created. What ami I doing wrong? Any help would be greatly appreciated. Thanks.

Reply With Quote
  #2 (permalink)  
Old December 28th, 2003, 07:50 AM
Imar's Avatar
Wrox Author
Points: 65,751, Level: 100
Points: 65,751, Level: 100 Points: 65,751, Level: 100 Points: 65,751, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 15,982
Thanks: 64
Thanked 1,351 Times in 1,331 Posts
Default

Hi Hugh,

How do you test this? That is, how do you "create" your files? When you copy an existing file into the watched directory, it will indicate the file has changed. However, when you save a Word document in the folder, or choose File | New | Text Document for example, you'll see the file marked as created.

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #3 (permalink)  
Old December 28th, 2003, 08:44 AM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar,

I am testing it the way you mentioned, i.e., I have created new files into the directory using both Notepad and Word. I have also used "Save As" as well as copying files into the directory. However, I never return a "Created" status. Also, I tried not converting the ChangeType to string and it returns a "4" every time. Additiaionally, in another iteration of the program, I tried setting up different subroutines with different handles clause, which also did not work. Not sure what is going on.

Reply With Quote
  #4 (permalink)  
Old December 28th, 2003, 09:41 AM
Imar's Avatar
Wrox Author
Points: 65,751, Level: 100
Points: 65,751, Level: 100 Points: 65,751, Level: 100 Points: 65,751, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 15,982
Thanks: 64
Thanked 1,351 Times in 1,331 Posts
Default

Really weird. I used the code you posted in an app and it works fine.

You may try to add the new text to the label, instead of overwriting the previous stuff. I found that when you save a Word document, the code fires multiple times, so you may end up with a Change for the latest event, overwriting a previous Create.

Other than that, I am stumped. It works as expected here....


Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #5 (permalink)  
Old December 28th, 2003, 10:51 AM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar,

What OS are you on? I am using Windows XP Pro for testing.

Reply With Quote
  #6 (permalink)  
Old December 28th, 2003, 11:05 AM
Imar's Avatar
Wrox Author
Points: 65,751, Level: 100
Points: 65,751, Level: 100 Points: 65,751, Level: 100 Points: 65,751, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 15,982
Thanks: 64
Thanked 1,351 Times in 1,331 Posts
Default

Same here. Windows XP Pro, SP1.

I tried the example in Visual Studio .NET 2003.... Haven't tried 2002 yet.

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #7 (permalink)  
Old December 28th, 2003, 11:50 AM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar,

As always, you have cut to the root of the problem. The problem was not in the FileSystemWatcher , but in the label structure. Since there were multiple events firing in both Notepad and Word documents, and it appears that the final event of any created file is a changed, I was only picking up changes. In any event, thanks for the help (as always).

Reply With Quote
  #8 (permalink)  
Old March 8th, 2004, 01:20 PM
Registered User
 
Join Date: Dec 2003
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Why won't the following work?

<%@ Page Language="VB" Debug="True" %>
<%@ import Namespace="System.IO" %>
<%@ import Namespace="System.Diagnostics" %>
<script runat="server">

    Sub Page_Load(sender as Object, e as EventArgs)
        Dim myWatcher As New System.IO.FileSystemWatcher

        Dim myPath As String = "C:\temp"

        myWatcher.Path = myPath

        myWatcher.EnableRaisingEvents = True

        AddHandler myWatcher.Created, AddressOf logchange

        AddHandler myWatcher.Changed, AddressOf logchange

    End Sub

    Public Sub logchange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
        If e.ChangeType = WatcherChangeTypes.Created Then
            Label1.Text = e.FullPath & " was created"
        ElseIf e.ChangeType = WatcherChangeTypes.Changed Then
            Label1.Text = e.FullPath & " was changed"
        End If

    End Sub

</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <asp:Label id="Label1" runat="server">Watching for changes to c:\temp...</asp:Label>
    </form>
</body>
</html>


Reply With Quote
  #9 (permalink)  
Old March 8th, 2004, 01:40 PM
Imar's Avatar
Wrox Author
Points: 65,751, Level: 100
Points: 65,751, Level: 100 Points: 65,751, Level: 100 Points: 65,751, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 15,982
Thanks: 64
Thanked 1,351 Times in 1,331 Posts
Default

I guess it *does* work, but only for a split-second (a pico second, on fast or tuned systems ;) )

The Page_Load event fires when the page loads. However, as soon as the page has finished loading, all variables declared inside the method body get out of scope and are destroyed. So, only during the Page_Load event at the server will this code (technically) work.

A FileSystemWatcher is more suitable in desktop or Service applications.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #10 (permalink)  
Old March 9th, 2004, 08:39 AM
Registered User
 
Join Date: Dec 2003
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay, I understand that. Now how about this? I use no page_load event, only "clickable" events. Still no go...

<%@ Page Language="VB" Debug="True" %>
<%@ import Namespace="System.IO" %>
<%@ import Namespace="System.Diagnostics" %>
<script runat="server">

              Public watchfolder As FileSystemWatcher

              Public objFSO = Server.CreateObject("Scripting.FileSystemObject")

              Public Sub btn_start_Click(sender As Object, e As EventArgs)
                 watchfolder = New System.IO.FileSystemWatcher()

    ' if objfso.FileExists(txt_watchpath.Text) Then
                  if objfso.FolderExists(txt_watchpath.Text) Then
                    'this is the path we want to monitor
                     watchfolder.Path = txt_watchpath.Text
                     txt_folderactivity.Text &= "Monitor Started...." & vbCrLf
                     txt_folderactivity.Text &= " Watching " & txt_watchpath.Text & "...." & vbCrLf

                  'Add a list of Filter we want to specify make sure you use OR for each Filter as we need to
                  'include all of those

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

                  ' 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

                     watchfolder.IncludeSubdirectories = False
                     watchfolder.EnableRaisingEvents = True
                   'Set this property to true to start watching
                     btn_startwatch.Enabled = False
                     btn_stop.Enabled = True
                  else
                     txt_folderactivity.Text = "Invalid directory: " & txt_watchpath.Text & ". Re-enter"
                  end if
              End Sub

              Public Sub btn_stop_Click(sender As Object, e As EventArgs)
                 txt_folderactivity.Text &= "Monitor Stopped..." & vbCrLf
                 watchfolder = New System.IO.FileSystemWatcher()
                 watchfolder.EnableRaisingEvents = False
                 btn_startwatch.Enabled = True
                 btn_stop.Enabled = False
              End Sub

              Public Sub logchange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
                 txt_folderactivity.Text &= "***Event Fired!***"

                 If e.ChangeType = IO.WatcherChangeTypes.Changed Then
                    txt_folderactivity.Text &= "File " & e.FullPath & " has been modified" & vbCrLf
                 End If

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

                 If e.ChangeType = IO.WatcherChangeTypes.Deleted Then
                    txt_folderactivity.Text &= "File " & e.FullPath & " has been deleted" & vbCrLf
                 End If
              End Sub

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

</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:Button id="btn_startwatch" onclick="btn_start_Click" runat="server" Text="Start Watching"></asp:Button>
        </p>
        <p>
            Type Folder to Watch:&nbsp;
            <asp:TextBox id="txt_watchpath" runat="server" Width="323px"></asp:TextBox>
            &nbsp;&nbsp;&nbsp;&nbsp;
            <asp:Button id="btn_stop" onclick="btn_stop_Click" runat="server" Text="Stop Watching"></asp:Button>
        </p>
        <p>
            Folder Activity:
        </p>
        <p>
            <asp:TextBox id="txt_folderactivity" runat="server" Width="759px" Height="409px" TextMode="MultiLine"></asp:TextBox>
        </p>
    </form>
</body>
</html>


Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
writing into a file in linux file system anboss XSLT 6 June 19th, 2008 01:56 PM
Help for directory watcher with Windows Service abhisjek-patel VB.NET 0 November 13th, 2007 01:22 AM
Can anybody help me in the system watcher project embursts VB.NET 0 January 29th, 2006 04:15 AM
File watcher with Windows service GregF VB.NET 1 February 1st, 2005 08:39 AM
File System Watcher - Part 2 hugh@kmcnetwork.com VB.NET 1 December 29th, 2003 03:40 PM



All times are GMT -4. The time now is 02:35 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.