Forum Moderators: open
I've got a theory question for you all.
My aim is to fire a c# method when a new file is added to a specific folder on the server (Windows Server). This code can be within a website, windows application or a webservice, etc, that is not my issue (not yet, anyway!).
What I'm struggling with is how do I model the application. From what I can see, I have two options -
1. Create an application that is constantly running, that keeps on checking the folder every so often, then calling the code when a new file is found.
2. Somehow create a trigger on the folder that somehow calls a method somewhere.
As you may notice, I don't really understand how I would do point 2!
So, my question - how has this been done before, and what have you learnt from doing this?
All suggestions would be greatly appreciated!
Thanks,
B
Reference:
MSDN FileSystemWatcher [msdn.microsoft.com]
Just a little follow-up - some of the files I'm waiting on can be quite large, and I'm finding it hard to work out when a file has been completely copied.
For example, when a new file is added, I get a CREATED event fired, and then two or three CHANGED events, a few seconds after the initial event (depending on the file size).
Have you (or anyone) figured out a good methodology for working out when a file has finished being copied into a folder?
Cheers,
B
FileStream fs = null;
try
{
// Attempt opening file
fs = File.Open(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.None);
}
catch
{
// Open file failed, probably still being written to
return;
}
finally
{
if (fs != null)
{
fs.Close();
}
}
// Otherwise, continue with normal operation
* Edit - Cleaned up code