File System Watcher problem

M

Mo

Him

I am using FileSystemWatcher in a Windows service on .Net 2.0 C#
application. When I see

ChageType="Created" I copy the file over to a database. About 30-40%
of the time I am getting a strange error which I am including below. I
am suspecting that the event is triggered before file is closed and
hence this error is happening. the symptom is that the file is not
copied to destination after the handler is done. Does anybody knwo how
to check to see if the file is closed before takign action on
FileSystemEventHandler is being fired?

here is what I have

protected override void OnStart(string[] args)
{
FSW.Changed += new FileSystemEventHandler(FSW_Changed);
FSW.Created += new FileSystemEventHandler(FSW_Changed);
FSW.Deleted += new FileSystemEventHandler(FSW_Changed);
FSW.Renamed += new RenamedEventHandler(FSW_Renamed);
FSW.Path =
ConfigurationSettings.AppSettings["HostDirectory"].ToString();
FSW.Filter = "*.*";
FSW.IncludeSubdirectories = true;
FSW.NotifyFilter = NotifyFilters.LastAccess |
NotifyFilters.LastWrite
| NotifyFilters.FileName |
NotifyFilters.DirectoryName;

//Begin monitoring.
FSW.EnableRaisingEvents = true;
}

This is the error I see in the log file

Event Type: Error
Event Source: .NET Runtime 2.0 Error Reporting
Event Category: None
Event ID: 5000
Date: 8/22/2007
Time: 7:48:56 PM
User: N/A
Computer: QS1-YI3T7F4BSGG
Description:
EventType clr20r3, P1 documentmanagement.exe, P2 1.0.0.0, P3 46cc97e9,
P4 mscorlib, P5 2.0.0.0, P6 4333ab80, P7 32f8, P8 21c, P9
system.io.ioexception, P10 NIL.
Data:
0000: 63 00 6c 00 72 00 32 00 c.l.r.2.
0008: 30 00 72 00 33 00 2c 00 0.r.3.,.
0010: 20 00 64 00 6f 00 63 00 .d.o.c.
0018: 75 00 6d 00 65 00 6e 00 u.m.e.n.
0020: 74 00 6d 00 61 00 6e 00 t.m.a.n.
0028: 61 00 67 00 65 00 6d 00 a.g.e.m.
0030: 65 00 6e 00 74 00 2e 00 e.n.t...
0038: 65 00 78 00 65 00 2c 00 e.x.e.,.
0040: 20 00 31 00 2e 00 30 00 .1...0.
0048: 2e 00 30 00 2e 00 30 00 ..0...0.
0050: 2c 00 20 00 34 00 36 00 ,. .4.6.
0058: 63 00 63 00 39 00 37 00 c.c.9.7.
0060: 65 00 39 00 2c 00 20 00 e.9.,. .
0068: 6d 00 73 00 63 00 6f 00 m.s.c.o.
0070: 72 00 6c 00 69 00 62 00 r.l.i.b.
0078: 2c 00 20 00 32 00 2e 00 ,. .2...
0080: 30 00 2e 00 30 00 2e 00 0...0...
0088: 30 00 2c 00 20 00 34 00 0.,. .4.
0090: 33 00 33 00 33 00 61 00 3.3.3.a.
0098: 62 00 38 00 30 00 2c 00 b.8.0.,.
00a0: 20 00 33 00 32 00 66 00 .3.2.f.
00a8: 38 00 2c 00 20 00 32 00 8.,. .2.
00b0: 31 00 63 00 2c 00 20 00 1.c.,. .
00b8: 73 00 79 00 73 00 74 00 s.y.s.t.
00c0: 65 00 6d 00 2e 00 69 00 e.m...i.
00c8: 6f 00 2e 00 69 00 6f 00 o...i.o.
00d0: 65 00 78 00 63 00 65 00 e.x.c.e.
00d8: 70 00 74 00 69 00 6f 00 p.t.i.o.
00e0: 6e 00 20 00 4e 00 49 00 n. .N.I.
00e8: 4c 00 0d 00 0a 00 L.....
 
J

jacky kwok

Mo said:
Him

I am using FileSystemWatcher in a Windows service on .Net 2.0 C#
application. When I see

ChageType="Created" I copy the file over to a database. About 30-40%
of the time I am getting a strange error which I am including below. I
am suspecting that the event is triggered before file is closed and
hence this error is happening. the symptom is that the file is not
copied to destination after the handler is done. Does anybody knwo how
to check to see if the file is closed before takign action on
FileSystemEventHandler is being fired?

here is what I have

protected override void OnStart(string[] args)
{
FSW.Changed += new FileSystemEventHandler(FSW_Changed);
FSW.Created += new FileSystemEventHandler(FSW_Changed);
FSW.Deleted += new FileSystemEventHandler(FSW_Changed);
FSW.Renamed += new RenamedEventHandler(FSW_Renamed);
FSW.Path =
ConfigurationSettings.AppSettings["HostDirectory"].ToString();
FSW.Filter = "*.*";
FSW.IncludeSubdirectories = true;
FSW.NotifyFilter = NotifyFilters.LastAccess |
NotifyFilters.LastWrite
| NotifyFilters.FileName |
NotifyFilters.DirectoryName;

//Begin monitoring.
FSW.EnableRaisingEvents = true;
}

This is the error I see in the log file


Yes, the "Created" is raised when the file is created. It is nothing
about the file is closed or not.

In my experience, try to open the file in exclusive mode. If the file
can be opened , then the file is not opened by other.

However this method still has some risk. In my experience, some
application will output file, processing the file, close the file, then
open the file again and some other process.

That mean even you can open the file exclusively at some certain time,
the file may be opened by other when you want to actually use it.

Hence, a better method is re-try. If you catch an error of file is used
by other process when try to move/copy/process file, re-try after
waiting a period.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top