Baran Topal

Baran Topal


September 2018
M T W T F S S
« Feb    
 12
3456789
10111213141516
17181920212223
24252627282930

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
  }
}