Home  |  Delphi .net Home  |  System.IO.FileSystemWatcher  |  WaitForChanged Method
WaitForChanged  
Method  
Synchronously wait for a file system change to take place
FileSystemWatcher Class
System.IO NameSpace
CF1.  Function WaitForChanged ( ChangeType : WatcherChangeTypes; ) : WaitForChangedResult;
NotCF2.  Function WaitForChanged ( ChangeType:WatcherChangeTypesChangeType : WatcherChangeTypes; WaitLimitMilliseconds : Integer; ) : WaitForChangedResult;
CF : Methods with this mark are Compact Framework Compatible
Description
The WaitForChanged method waits until the specified ChangeType occurs in the current path. The wait is indefinite unless the WaitLimitMiiilseconds value is specified.
 
The ChangeType must be one of the following :
 
All The creation, deletion, change, or renaming of a file or folder.
Changed The change of a file or folder. The types of changes include: changes to size, attributes, security settings, last write, and last access time.
Created The creation of a file or folder.
Deleted The deletion of a file or folder.
Renamed The renaming of a file or folder.

 
The returned WaitForChangedResult holds information about the change :
 
ChangeType The type of change that occurred.
Name The name of the file or directory that changed.
OldName The original name of the file or directory that was renamed.
TimedOut A value indicating whether the wait operation timed out.
Microsoft MSDN Links
System.IO
System.IO.FileSystemWatcher
 
 
Waiting for any file system access
program Project1;
{$APPTYPE CONSOLE}

uses
  System.IO;

var
  MyWatcher : System.IO.FileSystemWatcher;

begin
  // Create a FileSystemWatcher object
  MyWatcher := FileSystemWatcher.Create('C:\');

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

  // Now we wait until an event occurs
  MyWatcher.WaitForChanged(WatcherChangeTypes.Changed);

  // Indicate a file has been accessed
  Console.WriteLine('A file or folder has been accessed');

  Console.Readline;
end.
Show full unit code
  When a C:\ file or folder is accessed, the above code produces the following message :
  
  A file or folder has been accessed
Waiting for a file to be renamed
program Project1;
{$APPTYPE CONSOLE}

uses
  System.IO;

var
  MyWatcher    : System.IO.FileSystemWatcher;
  ChangeResult : System.IO.WaitForChangedResult;

begin
  // Create a FileSystemWatcher object
  MyWatcher := FileSystemWatcher.Create('C:\');

  // Watch for access to a file (which we will rename)
  MyWatcher.NotifyFilter := NotifyFilters.FileName;

  // Now we wait until an event occurs
  ChangeResult := MyWatcher.WaitForChanged(WatcherChangeTypes.Renamed);

  // Indicate a file has been renamed
  Console.WriteLine('{0} renamed to {1}',
                    ChangeResult.OldName,
                    ChangeResult.Name);

  Console.Readline;
end.
Show full unit code
  After renaming a file in the C:\ folder, the following message was produced :
  
  DelphiBasics.txt renamed to RenamedDelphiBasics.txt
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author