I understand that there are so many similar questions on Stackoverflow and other sites. I didn't quite get the answer I am looking for. Hence I make this attempt to ask the question.
I have a requirement to move files from SOURCE folder to DEST folder. And also check if the files have moved successfully.
I created a simple test harness with the following code
private void btnStartWatch_Click(object sender, EventArgs e)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = source;
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Filter = "*.*";
watcher.Changed += Watcher_Changed;
watcher.EnableRaisingEvents = true;
}
private void Watcher_Changed(object sender, FileSystemEventArgs e)
{
try
{
// Debug.WriteLine("File detected: " + e.Name);
File.Move(Path.Combine(source, e.Name), Path.Combine(target, e.Name));
}
catch (Exception ex)
{
// do not throw ex for now.
// I get into this block several times even for a single file??
}
}
The above code seems to do the trick. However I do not know how many files have been moved. How do I check if the files arrived at the SOURCE have been moved to DEST?
There is no completion event on FileSystemWatcher, so I thought I can use BackgroundWorker instead. What do you guys think?
The background worker gets Triggered every 10 Seconds. Checks
Directory.GetFiles to a get a list of files (list x) at SOURCE
- Go through each file and check if they exist at DEST?
- If the file do not exist Move it Finally go through the list (list X) and check all files are present in DEST.