70-536 Chapter02 Lesson1 Question2 FileSystemWatcher vs DIR?

8 views
Skip to first unread message

Spyde...@comcast.net

unread,
May 20, 2009, 11:27:12 PM5/20/09
to GLUG.net-Certification Study Group
The question was "Does the FileSystemWatcher object see the creation
of Directories?"

In short my test, found that it did not. However it did find the
files created in that directory. So a FileSystemWatcher crated to
watch c:\temp would find c:\temp\test.txt, but not c:\temp\New Folder
\ It would also find the c:\temp\New Folder\test.txt. So question
number 2 at the bottom of page77 seems to have the answer B and C
thus the book on page 711 seems to be wrong.

My test was run on a fully patched version of Vista Ultimate and
VS2008


I took this a step further and found the following msdn docs on the
subject.
"FileSystemWatcher Class", "Listens to the file system change
notifications and raises events when a directory, or file in a
directory, changes."
http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx

"FileSystemWatcher Members", "Created" "Occurs when a file or
directory in the specified Path is created."
http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher_members.aspx

Clearly Microsoft intends on this running and thus the two of us who
have seen this issue may want to assume that the test will be written
in line with the documentation.

Jay Harris

unread,
May 21, 2009, 7:12:27 PM5/21/09
to GLUG.net-Certification Study Group
In my tests, the FileSystemWatcher did see sub-directory creation, and
worked as expected. When monitoring "C:\Temp\", this included seeing
the creation/modification of "C:\Temp\New Folder\Test.txt" only when
FileSystemWatcher.IncludeSubDirectories was set to true.
However, I am running on Windows 7, so perhaps it is an OS issue.
Would you mind sending a copy of your sample code, so that I can test
it out in Windows 7 in the spirit of comparing apples to apples?

The code I am using:
(C:\Temp\ must already exist, and your account must have credentials
to modify it, or else an exception will be thrown.)

//============================

using System;
using System.IO;

namespace FileSystemWatcherSample
{
class Program
{
static void Main(string[] args)
{
FileSystemWatcher fsWatch = new FileSystemWatcher(@"C:
\Temp");
fsWatch.Created += FsWatchCreated;
fsWatch.Changed += FsWatchChanged;
fsWatch.Deleted += FsWatchDeleted;
fsWatch.Renamed += FsWatchRenamed;
fsWatch.EnableRaisingEvents = true;
fsWatch.IncludeSubdirectories = true;

Console.ReadLine();

fsWatch.EnableRaisingEvents = false;
}

private static void FsWatchRenamed(object sender,
RenamedEventArgs e)
{
Console.Out.WriteLine("Renamed: " + e.FullPath);
}
private static void FsWatchDeleted(object sender,
FileSystemEventArgs e)
{
Console.Out.WriteLine("Deleted: " + e.FullPath);
}
private static void FsWatchChanged(object sender,
FileSystemEventArgs e)
{
Console.Out.WriteLine("Changed: " + e.FullPath);
}
private static void FsWatchCreated(object sender,
FileSystemEventArgs e)
{
Console.Out.WriteLine("Created: " + e.FullPath);
}
}
}

//==========================================

--
Jay Harris


On May 20, 11:27 pm, "Spyder2...@comcast.net" <Spyder2...@comcast.net>
wrote:
> The question was "Does the FileSystemWatcher object see the creation
> of Directories?"
>
> In short my test, found that it did not.  However it did find the
> files created in that directory.  So a FileSystemWatcher crated to
> watch c:\temp would find c:\temp\test.txt, but not c:\temp\New Folder
> \  It would also find the c:\temp\New Folder\test.txt.  So question
> number 2 at the bottom of page77 seems to have the answer  B and C
> thus the book on page 711 seems to be wrong.
>
> My test was run on a fully patched version of Vista Ultimate and
> VS2008
>
> I took this a step further and found the following msdn docs on the
> subject.
> "FileSystemWatcher Class", "Listens to the file system change
> notifications and raises events when a directory, or file in a
> directory, changes."http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx
>
> "FileSystemWatcher Members", "Created" "Occurs when a file or
> directory in the specified Path is created."http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher_m...

Spyde...@comcast.net

unread,
May 21, 2009, 11:34:40 PM5/21/09
to GLUG.net-Certification Study Group
Jay,
Sure, I changed the declaration of the object so that the path was "c:
\temp". So it seems that VB and C# are the only real differences. I
am an admin on this pc and the owner of the files / directories. Hope
this helps.

Imports System.IO
Module Module1

Sub Main()
' Create an instance of FileSystemWatcher
'Dim fsw As New FileSystemWatcher
(Environment.GetEnvironmentVariable("USERPROFILE"))
Dim fsw As New FileSystemWatcher("c:\temp")
' Set the FileSystemWatcher properties
fsw.IncludeSubdirectories = True
fsw.NotifyFilter = NotifyFilters.FileName Or
NotifyFilters.LastWrite

' Add the Changed event handler
AddHandler fsw.Changed, AddressOf fsw_Changed
AddHandler fsw.Created, AddressOf fsw_Changed
AddHandler fsw.Deleted, AddressOf fsw_Changed
AddHandler fsw.Renamed, AddressOf fsw_Renamed

' Start monitoring events
fsw.EnableRaisingEvents = True

' Wait for user input before ending
Console.WriteLine("Press a key to end the program.")
Console.ReadKey()
End Sub

Sub fsw_Changed(ByVal sender As Object, ByVal e As
FileSystemEventArgs)
' Write the path of a changed file to the console
Console.WriteLine(e.ChangeType.ToString + ": " + e.FullPath)
End Sub

Sub fsw_Renamed(ByVal sender As Object, ByVal e As
RenamedEventArgs)
' Write the path of a changed file to the console
Console.WriteLine(e.ChangeType.ToString + " from " +
e.OldFullPath + " to " + e.Name)
End Sub
> > in line with the documentation.- Hide quoted text -
>
> - Show quoted text -
Message has been deleted

Jay Harris

unread,
May 22, 2009, 12:53:43 PM5/22/09
to glug-cert...@googlegroups.com
Paul,

As with all Google Group messages, unsubscribe information is included at the bottom of the email.

--
Jay Harris



On Fri, May 22, 2009 at 12:23 PM, Paul Emery <theoa...@lansing.com> wrote:

Hi,
Can someone take my address off this email list. I am no longer
participating.
Thanks
Paul Emery
theoa...@lansing.com
__________ Information from ESET Smart Security, version of virus signature
database 4097 (20090522) __________

The message was checked by ESET Smart Security.

http://www.eset.com







Reply all
Reply to author
Forward
0 new messages