Hi Allen,
Sorry for letting you wait for so long time.
Based on your feedback, you only want to be notified when your file
creation is over.
=============================================
Althrough FileSystemWatcher class does not expose a closed event, but I
think you can just use Changed event to get you want.
After a process created a file and finished writing data into the new
created file, it will invoke CloseHandle to release the file handle. Also,
at this time, the file's lastwrite attribute will be modified, then the
FileSystemWatcher.Changed event will fire. So you can just use the Changed
event to determine the creating over of the file.
Like this:
private void button1_Click(object sender, System.EventArgs e)
{
StreamWriter sw = new StreamWriter(@"D:\TestFile.txt") ;
System.Threading.Thread.Sleep(5000);
for(int i=0;i<10000;i++)
{
// Add some text to the file.
sw.Write("This is the ");
sw.WriteLine("header for the file.");
sw.WriteLine("-------------------");
// Arbitrary objects can also be written to the file.
sw.Write("The date is: ");
sw.WriteLine(DateTime.Now);
}
sw.Close();
}
private void Form1_Load(object sender, System.EventArgs e)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"D:\";
watcher.Filter = @"*.txt";
watcher.Created+=new FileSystemEventHandler(watcher_Created);
watcher.Changed+=new FileSystemEventHandler(watcher_Changed);
watcher.EnableRaisingEvents = true;
}
private void watcher_Created(object sender, FileSystemEventArgs e)
{
Console.WriteLine("watcher_Created");
}
private void watcher_Changed(object sender, FileSystemEventArgs e)
{
Console.WriteLine("watcher_Changed");
}
After some consulting and researching, I was told that .Net
FileSystemWatcher class actually encapsulate the Win32 API
ReadDirectoryChangesW(), there is a 1:1 relationship between them. So the
FileSystemWatcher class can only provide the function that
ReadDirectoryChangesW() expose.
=================================================
If you still have anything unclear, please feel free to tell me, I will
help you.
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.