Rad,
Are you looking for the deleted and rename events? If so, I think you need
attach the event handler to those events. Since the "Changed" only fires
when the object changes, when I used your code I did see a behavior where
it appears that fsw_changed "misses" events. Attaching the handler to the
additional events seemed to correct the behavior for me. I also added
explicitly set the "EnableRaisingEvents" property to true.
See code below.
H.
using System;
using System.Collections.Generic;
using System.IO;
namespace FileSystemWatcherExample
{
class Program
{
static void WaitForKey()
{
Console.WriteLine("Any Key To Exit");
Console.ReadLine();
}
static void Main(string[] args)
{
FileSystemWatcher fsw = new FileSystemWatcher("C:\\FSWTest", "*.txt");
fsw.EnableRaisingEvents = true;
fsw.IncludeSubdirectories = true;
fsw.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Attributes |
NotifyFilters.LastAccess | NotifyFilters.LastWrite |
NotifyFilters.Security | NotifyFilters.Size;
//...Here are the additional events attached to handler
fsw.Changed += new FileSystemEventHandler(fsw_Changed);
fsw.Renamed += new RenamedEventHandler(fsw_Changed);
fsw.Deleted += new FileSystemEventHandler(fsw_Changed);
fsw.Created += new FileSystemEventHandler(fsw_Changed);
fsw.InternalBufferSize = 65536;
fsw.EnableRaisingEvents = true;
WaitForKey();
}
static void fsw_Changed(object sender, FileSystemEventArgs e)
{
Dictionary<WatcherChangeTypes, string> eventMap = new
Dictionary<WatcherChangeTypes, string>{
{WatcherChangeTypes.Changed, "Changed"},
{WatcherChangeTypes.Created, "Created"},
{WatcherChangeTypes.Deleted, "Deleted"},
{WatcherChangeTypes.Renamed, "Renamed"},
{WatcherChangeTypes.All, "All"},
};
string action;
if (!eventMap.TryGetValue(e.ChangeType, out action))
action = "Unknown Event";
Console.WriteLine("Path: " + e.FullPath + "; File Action: " + action);
}
}
}
--
You received this message because you are subscribed to the Google
Groups "DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML
Web Services,.NET Remoting" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/dotnetdevelopment?hl=en?hl=en
or visit the group website at http://megasolutions.net