Camillia
asked on
Filewatcher - checking for file completed using this example
I have a file watcher code that actually someone here (Eric - emaru?) helped me with. I need to make a change to the code and not sure how to do it.
I have this example:
http://stackoverflow.com/questions/4277991/c-sharp-filesystemwatcher-how-to-know-file-copied-completely-into-the-watch-fol
If you scroll down, i want to do this;
I subscribe to the Changed- and Renamed-event and try to rename the file on every Changed-event catching the IOExceptions. If the rename succeeds, the copy has finished and the Rename-event is fired only once.
Based on this http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx
I need:
1. watcher.Renamed += new RenamedEventHandler(OnRena med);
2. what do I need in OnRenamed?3. watcher.Changed += new FileSystemEventHandler(OnC hanged);
4. what do I need in OnChanged here?
I have this example:
http://stackoverflow.com/questions/4277991/c-sharp-filesystemwatcher-how-to-know-file-copied-completely-into-the-watch-fol
If you scroll down, i want to do this;
I subscribe to the Changed- and Renamed-event and try to rename the file on every Changed-event catching the IOExceptions. If the rename succeeds, the copy has finished and the Rename-event is fired only once.
Based on this http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx
I need:
1. watcher.Renamed += new RenamedEventHandler(OnRena
2. what do I need in OnRenamed?3. watcher.Changed += new FileSystemEventHandler(OnC
4. what do I need in OnChanged here?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You could. I usually add the filename to a list in the event and have another thread looping through the list checking completeness and if so handling the file. But it depends on the amount of files and work per each file.
You could. I usually add the filename to a list in the event and have another thread looping through the list checking completeness and if so handling the file. But it depends on the amount of files and work per each file.
oops, I managed to doublepost.
ASKER
trying this now. Might need help with thread looping you mention. The files get copied to the server maybe twice a day. Not a constant file copying. Let me try this and will post back.
Well In that case you might not need the threading. I'm thinking of situations where you have a lot more constantly incoming files.
ASKER
Ok, your code works but now I dont know how I can recheck the file-completion copy and do whatever I need to do with it.
This is my process:
1. I copy a file from one server to another, server2
2. When file is on server2, I call a stored proc to import the file. That's it.
---- This is what I have now
1. I copy a file from one server to another, server2
2. Check if file is copied on server2 in onChanged event
3. Now, how do I get back to checking if it's completed and import it??
This is my code. I have your code in step 4
This is my process:
1. I copy a file from one server to another, server2
2. When file is on server2, I call a stored proc to import the file. That's it.
---- This is what I have now
1. I copy a file from one server to another, server2
2. Check if file is copied on server2 in onChanged event
3. Now, how do I get back to checking if it's completed and import it??
This is my code. I have your code in step 4
//when file is first copied to NAS server
if (server == "NAS")
{
watcher.Created += new FileSystemEventHandler(OnChangedGP);
(OnRenamedGP);
}
else
watcher.Created += new FileSystemEventHandler(OnChanged);
//2. Move the file from NAS to the other server on onChanged event
private void OnChangedGP(object source, FileSystemEventArgs e)
{
string fileName = e.Name;
try
{
switch (fileName)
{
case "NXB.txt":
MoveFile(fileName, "NAS");
break;
default:
break;
}
}
catch (Exception)
{
}
}
//3. Moves the file to second server
private void MoveFile(string filename, string flag)
{
//move file to server2 here
}
//4. File is on server 2. Checks to see if file copy is complete. I got a "false" but then it never got back here to actually import it
private void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
string fileName = e.Name;
try
{
switch (fileName)
{
....
case "NXB.txt":
bool fileCopyComplete = IsFileClosed(fileName);
if (fileCopyComplete) // i get false here but that's it. Then file is on server2..when would I get to import it then??
{
ImportFile("NXB", 0);
MoveFile(fileName, "GP");
}
break;
default:
break;
}
}
catch (Exception ex)
{
Log(e.Name, ex.Message);
}
}
ASKER
If IsFileClosed is false, then the file isn't completed. Then you have to wait until the file is completed. You could try next file in list, without removing the uncompleted file. So you'll try that file on next loop again or you could use a method like
to wait for the file to complete.
public static bool IsFileClosed(string filename, int retries, int sleepms) {
bool result = false;
while (retries-- > 0) {
if (IsFileClosed(filename)) {
result = true;
break;
}
System.Threading.Thread.Sleep(sleepms);
}
return result;
}
to wait for the file to complete.
ASKER
yes, found this example. The site is a bit slow to load. I think this should work. Similar to yours
http://bloggingabout.net/blogs/jschreuder/archive/2006/07/06/12886.aspx
http://bloggingabout.net/blogs/jschreuder/archive/2006/07/06/12886.aspx
Is it working now or do you need some more advice?
ASKER
I tested it yesterday and it seems like it's working. Going to close this question now. Thanks for your help.
ASKER