Home  |  Delphi .net Home  |  System.IO.FileSystemWatcher  |  OnChanged Method
OnChanged  
Method  
A method that is called when a file system change is detected
FileSystemWatcher Class
System.IO NameSpace
NotCF1.  Procedure OnChanged ( Event : FileSystemEventArgs; ) ;
CF : Methods with this mark are Compact Framework Compatible
Description
The OnChanged method is called when the Changed event is triggered.
Notes
The author was unable to get the OnChanged method invoked in the sample code. Am awaiting a response from Borland to help on this matter.
Microsoft MSDN Links
System.IO
System.IO.FileSystemWatcher
 
 
A simple example
program Project1;
{$APPTYPE CONSOLE}

uses
  System.IO;

// Class definition code
type
  MyWatcherClass = Class(System.IO.FileSystemWatcher)

    published
      procedure OnChanged (Events : FileSystemEventArgs);
  end;

var
  MyWatcher : MyWatcherClass;

procedure MyWatcherClass.OnChanged(Events: FileSystemEventArgs);
begin
  // It is crucial that the inherited OnChanged method is called
  inherited OnChanged(Events);

  // Indicate that we have detected a file or folder change
  Console.WriteLine(Events.FullPath + ' has changed');
end;

// Main code
begin
  // Create an object inherited from the FileSystemWatcher class
  MyWatcher := MyWatcherClass.Create('C:\');

  // Watch for any file access at all
  MyWatcher.NotifyFilter := NotifyFilters.LastAccess;

  // Now we wait until an event occurs -
  // MyWatcher will notify us of changes
  MyWatcher.WaitForChanged(WatcherChangeTypes.Changed);

  Console.WriteLine('Done');

  Console.Readline;
end.
Show full unit code
  It was expected that when accessing a file in the C:\ folder would have triggered the OnChanged method, but this was not the case.
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author