I'm trying to make a Service using FileSystemWatcher to detect some changes in my C drive.
The following code is not triggering, and I'm not sure why.
FileSystemWatcher watcher;
protected override void OnStart(string[] args)
{
trackFileSystemChanges();
watcher.EnableRaisingEvents = true;
}
The trackFileSystemChanges() method basically sets watcher to watch for changes in LastWrite and LastAccess time, the creation, deletion, or renaming of text files in the directory.
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public void trackFileSystemChanges()
{
watcher = new FileSystemWatcher();
watcher.Path = @"C:\";
Library.WriteErrorLog(watcher.Path);
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Filter = "*.*";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
}
When txt files are changed or renamed, the log gets written to a file.
private static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
Library.WriteErrorLog("File: " + e.FullPath + " " + e.ChangeType);
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
// Specify what is done when a file is renamed.
Library.WriteErrorLog("File: " + e.OldFullPath + "renamed to " + e.FullPath);
}
There is no problem with the Library.WriteErrorLog method as I've tested it with other stuff. When the service is started, and when I tried editing/renaming some txt files within my C drive, nothing gets logged.