
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Waiting for an Action to Occur in the Filesystem
|
741
string[] data = (string[])stateInfo;
// Wait a sec...
Thread.Sleep(1000);
// Create a file in the temp directory.
string path = data[0];
string file = path + data[1];
Console.WriteLine("Creating {0} in PauseAndCreateFile...",file);
using (FileStream fileStream = File.Create(file))
{
// Use fileStream var...
}
}
catch(Exception e)
{
Console.WriteLine(e.ToString( ));
throw;
}
}
Discussion
The WaitForChanged method returns a WaitForChangedResult structure that contains
the properties listed in Table 12-12.
The way we are currently making the
WaitForChanged call could possibly block indefi-
nitely. To prevent you from hanging forever on the
WaitForChanged call, you can
specify a timeout value of 3 seconds as follows:
WaitForChangedResult result =
fsw.WaitForChanged(WatcherChangeTypes.Created, 3000);
See Also
See the “FileSystemWatcher Class,” “NotifyFilters Enumeration,” and “Wait-
ForChangedResult Structure” topics in the MSDN documentation.
Table 12-12. WaitForChangedResult properties
Property Description
ChangeType Lists the type of change that occurred. This change is returned as a
WatcherChangeTypes enumera-
tion. The values of this enumeration can possibly be ORed together.
Name Holds the name of the file or directory that was changed. ...