T
TD
I'm watching a folder for new files. When the files arrive, I want to
wait until it is done being written to and then move it.
Every time I move a file into the test folder, it triggers 3 calls to
the OnChanged handler.
I can't tell what is changing between the 2nd and 3rd call to know
whether it is safe to move the file yet? If I was certain it was
always 3, I could go then but I'm not sure that it will always be 3
calls.
I've Console.Writeline'd everything that I can think of but noting
appears to be changing.
Any ideas? I've included the necessary code blocks
** WATCH THE FOLDER ***
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = "c:\\temp\\xml";
watcher.IncludeSubdirectories = false;
watcher.NotifyFilter = NotifyFilters.LastAccess |
NotifyFilters.LastWrite |
NotifyFilters.FileName |
NotifyFilters.DirectoryName |
NotifyFilters.Size;
// Only watch xml files.
watcher.Filter = "*.xml";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
// Begin watching.
watcher.EnableRaisingEvents = true;
// Wait for the user to quit the program.
Console.WriteLine("Press \'q\' to quit the sample.");
while(Console.Read()!='q');
private static void OnChanged(object source, FileSystemEventArgs e)
{
FileInfo fi = new FileInfo(e.FullPath);
if(fi.LastWriteTime >= DateTime.Now)
{
Console.WriteLine("still writing");
}
else
{
Console.WriteLine("done writing");
// MOVE THE FILE
// can't leave like this because of multiple calls
fi.MoveTo("c:\\temp\\xml_to");
}
}
wait until it is done being written to and then move it.
Every time I move a file into the test folder, it triggers 3 calls to
the OnChanged handler.
I can't tell what is changing between the 2nd and 3rd call to know
whether it is safe to move the file yet? If I was certain it was
always 3, I could go then but I'm not sure that it will always be 3
calls.
I've Console.Writeline'd everything that I can think of but noting
appears to be changing.
Any ideas? I've included the necessary code blocks
** WATCH THE FOLDER ***
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = "c:\\temp\\xml";
watcher.IncludeSubdirectories = false;
watcher.NotifyFilter = NotifyFilters.LastAccess |
NotifyFilters.LastWrite |
NotifyFilters.FileName |
NotifyFilters.DirectoryName |
NotifyFilters.Size;
// Only watch xml files.
watcher.Filter = "*.xml";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
// Begin watching.
watcher.EnableRaisingEvents = true;
// Wait for the user to quit the program.
Console.WriteLine("Press \'q\' to quit the sample.");
while(Console.Read()!='q');
private static void OnChanged(object source, FileSystemEventArgs e)
{
FileInfo fi = new FileInfo(e.FullPath);
if(fi.LastWriteTime >= DateTime.Now)
{
Console.WriteLine("still writing");
}
else
{
Console.WriteLine("done writing");
// MOVE THE FILE
// can't leave like this because of multiple calls
fi.MoveTo("c:\\temp\\xml_to");
}
}