FileSystemWatcher isimli net sınıfı ile belli bir konum altındaki dosya değişimleri (yazma, güncelleme, silme vb) takip edilebiliyor. Aşağıda örnek kod mevcut.
namespace FileWatcher
{
class Program
{
static void Main(string[] args)
{
FileSystemWatcher watch = new FileSystemWatcher();
watch.Path = @”C:\Program Files\Sony Ericsson\Update Service”;
watch.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
//// Only watch text files.
//watch.Filter = “*.txt”;
watch.Changed += new FileSystemEventHandler(OnChanged);
watch.Created += new FileSystemEventHandler(OnChanged);
watch.Deleted += new FileSystemEventHandler(OnChanged);
watch.Renamed += new RenamedEventHandler(OnRenamed);
watch.EnableRaisingEvents = true;
Console.ReadLine();
}
private static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
//if (e.FullPath == @”D:\tmp\p.txt”)
Console.WriteLine(“File: ” + e.FullPath + ” ” + e.ChangeType);
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
// Specify what is done when a file is renamed.
//if (e.FullPath == @”D:\tmp\p.txt”)
Console.WriteLine(“File: {0} renamed to {1}”, e.OldFullPath, e.FullPath);
}
}
}