using
System;
using
System.IO;
public
class
watcher
{
public
static
void
Main(
string
[] args)
{
if
(args.Length!=1)
{
Console.WriteLine(
"usage:Watcher.exe(directory)"
);
return
;
}
FileSystemWatcher watcher=
new
FileSystemWatcher();
watcher.Path=args[O];
watcher.NotifyFilter=NotifyFilters.LastAccess | NotifyFilters.LastWrite |
NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Filter=
"*.txt"
;
watcher.Changed +=
new
FileSystemEventHandler(OnChanged);
watcher.Created +=
new
FileSystemEventHandler(OnChanged);
watcher.Deleted +=
new
FileSystemEventHandler(OnChanged) ;
watcher.Renamed +=
new
RenamedEventHandler(OnRenamed);
watcher.EnableRaisingEvents=
true
;
Console.WriteLine(
"Press\'q\' to quit the sample."
);
while
(Console.Read()!=
'q'
);
}
public
static
void
OnChanged(
object
sender,FileSystemEventArgs e)
{
Console.WriteLine(
"file:"
+e.FullPath+
""
+e.ChangeType);
}
public
static
void
OnRenamed(
object
sender,RenamedEventArgs e)
{
Console.WriteLine(
"Fi]e:{0} renamed to{1}"
,e.OldFullPath,e.FullPath);
}
}