filewatcher doesnt work when copying across WAN
I have a Windows service that scans a particular folder and whenever a file is created, it fires off a DTS process on the SQL server. This works fine when I create a file on the server directly.
However, if I open up WIndows explorer on a remote machine with xx.xx.xx.xx\foldername and then copy it onto the server remotely, nothing happens. Is this a different event that I have to track?
Also, when is the created event fired because I have some large Excel files that might be firing the events at the wrong stages unless it only happens when it has been fully created?
ANy other solutions?
[620 byte] By [
JACKWEBS] at [2007-11-20 11:09:48]

# 1 Re: filewatcher doesnt work when copying across WAN
What events are you currently looking for?
Arjay at 2007-11-9 11:36:20 >

# 2 Re: filewatcher doesnt work when copying across WAN
OnCreated
private void SetupWatcher()
{
// Create a new FileSystemWatcher and set its properties.
watcher.Path = ConfigurationSettings.AppSettings["directoryToMonitor"];
//Watch for changes in FileName
watcher.NotifyFilter = NotifyFilters.FileName;
//Watch for all files.
watcher.Filter = "";
//Add event handlers.
watcher.Created += new FileSystemEventHandler(OnCreated); //Begin watching
watcher.EnableRaisingEvents = true;
}
public void OnCreated(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is created.
EventLog.WriteEntry("DTS Service for CCApp","File: " + e.FullPath + " " + e.ChangeType);
ProcessDir(ConfigurationSettings.AppSettings["directoryToMonitor"]);
}
Just wondering when the OnCreated event happens. Is it when the file is 100% copied and can it happen while it is still being copied?
If so, would it be better to make the application sleep for 30 seconds or so?
# 3 Re: filewatcher doesnt work when copying across WAN
Created -- This event is fired when a file is created in the directory that is being monitored. If you are planning to use this event to move the file that was created, you must write some error handling in your event handler that can handle situations where the file is currently in use by another process. The reason for this is that the Created event can be fired before the process that created the file has released the file. This will cause exceptions to be thrown if you have not prepared the code correctly.
So, what do I do ?
# 4 Re: filewatcher doesnt work when copying across WAN
Any suggestions?
Thanks
# 5 Re: filewatcher doesnt work when copying across WAN
I tried to find some code I wrote a while back on how to handle this. I couldn't find it, but if I remember correctly, upon receiving the on change event, you can attempt to open the file. If you can open it, you are done, if you can't, the file operation is still in progress. Yeah it was a hack.
Arjay at 2007-11-9 11:40:24 >

# 6 Re: filewatcher doesnt work when copying across WAN
Why don't they add some more events to the file system then ?! :)
Anyway, so could I make the application sleep for a while say a minute or so (should be enough of a bodge for the moment?
How would I do that?
# 7 Re: filewatcher doesnt work when copying across WAN
When a file is created I believe its Offline attribute is set until the data has finished being written. You can check that attribute in a loop with a sleep or with a Timer until its not set and then you're good to go.
# 8 Re: filewatcher doesnt work when copying across WAN
How do I check the offline attribute with the code above?
..because I get an array of files but then the fileinfo set is an array as well isn't it?
# 9 Re: filewatcher doesnt work when copying across WAN
Another thing you can do is to create the file with a .temp file extension and then rename (move) it to the proper file extension once all writing is done. You need to look for the rename event if you use that technique.
# 10 Re: filewatcher doesnt work when copying across WAN
The FileInfo class has an Attributes property, the documentation for which has a code example of its use. The File class has a GetAttributes method, the documentation for which has a code example of its use. Who'd have thought the Help could be so helpful?