Reputation: 904
Am using a file watcher to monitor a folder for new incoming folders with 6- csv files within.But file watcher created event fired before the new folder completetly copied.
the code in wacher_created method is:-
manifest_watcher.Created += new FileSystemEventHandler(manifest_watcher_Created);
manifest_watcher.EnableRaisingEvents = true;
public void manifest_watcher_Created(object sender, FileSystemEventArgs e)
{
foreach (string file in Directory.GetFiles(e.FullPath, "*.csv"))
{
FileInfo subFileInfo = new FileInfo(file);
logs.writeLog("FileInfo" + subFileInfo.FullName+"Name:"+subFileInfo.Name);
}
}
Though the incoming folder had 6 files above log written for only 2 files rest are left unnoticed.I want to make wacher wait until copying/folder creation completed before the action is taken that too Without using threads. How to accomplish this?
Upvotes: 0
Views: 1107
Reputation: 6276
If you read the documentation OnCreated event will be fired when when a file is created, changed, or deleted in the defined folder and e.FullPath
will return the full path of the changed file.
This means for every file created, changed or deleted in folder an event will be fired.
Your code should be little different and you do not need to wait for all the files to be written to destination folder
public void manifest_watcher_Created(object sender, FileSystemEventArgs e)
{
WatcherChangeTypes wct = e.ChangeType;
if (wct == WatcherChangeTypes.Created)
{
logs.writeLog("FileInfo" + e.FullPath + "Name:" + e.Name);
}
}
If you want to add filter for CSV file
then you can add it easily by checking the extension of created file before writing the logs.
Upvotes: 1
Reputation: 11763
You could create a timer, wait for your condition (6 files written) in a loop , and sleep for a short time if that condition hasn't been met ...
Upvotes: 1