Many times a day I receive xml-files from different users. These are FTPed to a folder on my drive, say D:\batch. As today I check these files with a Scheduled Task which starts an ASP.NET page and processes the files found and answers the uploader and ends. This task is run every 15 minutes, but the uploaders wants the answers quicker, so I'm trying to accomplish this. I've created a Windows Service which monitors a folder, and when a file is created, it processes it.
I've followed a couple of different guides, but espically this one http://www.codeproject.com/Articles/32591/Creating-a-Windows-Service-for-Watching-System-Dir
With the first file it works, but everytime the second file is added, it crashes:
Exception Info: System.IO.IOException
Stack:
at System.IO.__Error.WinIOError(Int32, System.String)
at System.IO.File.InternalCopy(System.String, System.String, Boolean, Boolean)
at System.IO.File.Copy(System.String, System.String)
at FileMonitor.FilKigger.Watcher_Created(System.Object, System.IO.FileSystemEventArgs)
at System.IO.FileSystemWatcher.OnCreated(System.IO.FileSystemEventArgs)
at System.IO.FileSystemWatcher.NotifyFileSystemEventArgs(Int32, System.String)
at System.IO.FileSystemWatcher.CompletionStatusChanged(UInt32, UInt32, System.Threading.NativeOverlapped*)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)
How come is this?
Some code:
protected override void OnStart(string[] args)
{
FileSystemWatcher Watcher = new FileSystemWatcher();
Watcher.Path = "D:\\FTP\\Batch\\";
Watcher.IncludeSubdirectories = true;
Watcher.Created += new FileSystemEventHandler(Watcher_Created);
Watcher.Deleted += new FileSystemEventHandler(Watcher_Deleted);
Watcher.EnableRaisingEvents = true;
}
And here just the code which copies the file to a new folder.
void Watcher_Created(object sender, FileSystemEventArgs e)
{
File.Copy(e.FullPath, "D:\\newFolder\\" + e.Name);
Lg.WriteEntry("File moved", EventLogEntryType.Information);
}
Catching the exception just leaves the file, but ofcourse keeps the service running.
Changed the watcher to find "_finish.txt"-files, which the user sends after the real file is uploaded
Watcher = new FileSystemWatcher(ftpFolder, "*_finish.txt");
Then replacing the _finish with "", and I've got the real file which is guaranteed to be finished uploading...