FileSystemWatcher is not working on a file that is being written to by another program. Every time this file is written to, a new line is added. I want to know when this occurs. I have tried to implement the FileSystemWatcher, however, it does not alert me every time a change occurs. I know when the file has been written to, so I know that the watcher has missed alerting me. I created a button to check the file size. When the button is clicked (after knowing the file has been written to) the alert finally triggers. I added an Error to the watcher to try and check for the buffer being full, but that does not appear to be the case. I think it has something to do with the fact the file is still able to be written to by the other program. Any advice would be appreciated. CreateFileWatcher is ran when the program is started. The path shown is not the true path. I just want to be alerted when the file has been written to.
public void CreateFileWatcher()
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"C:\Program Files\test";
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.Size;
watcher.Filter = "";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Error += new ErrorEventHandler(LogBufferError);
watcher.EnableRaisingEvents = true;
}
private void OnChanged(object sender, FileSystemEventArgs e)
{
MessageBox.Show("SomethingChanged");
}
void LogBufferError(object sender, ErrorEventArgs e)
{
string log = string.Format("{0:G} | Buffer limit exceeded", DateTime.Now);
MessageBox.Show("BufferError");
}