Baran Topal

Baran Topal


April 2024
M T W T F S S
« Feb    
1234567
891011121314
15161718192021
22232425262728
2930  

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