Baran Topal

Baran Topal


December 2023
M T W T F S S
« Feb    
 123
45678910
11121314151617
18192021222324
25262728293031

Categories


FileWatcher vs. well, manual handling

baranbaran

Well, one can easily check the state of a file (whether it is used or not) via FileWatcher in C#.NET. Following is more handy if you want to handle it manually and need to log it.


// create an event
public delegate void FileUpdatedEventHandler (object sender, EventArgs e);

public event FileUpdateEventHandler Updated;

private _lastWrite = 0;
private void CheckFileChanged () {
  string myFile = @"C:\myconnections.txt";
  FileInfo fi = new FileInfo(myFile);
  if (_lastWrite == 0) {
    _lastWrite = fi.LastWriteTimeUtc;
  }
  else if (_lastWrite != fi.LastWriteUtc) {
    if (Updated != null)
      // fire off the event
      Updated(this, EventArgs.Empty); // change this if you want to pass some data
  }
}