FileSystemWatcher

W

Waldy

Hi there,
I cannot get the FileSystemWatcher to work. I want to monitor
a directory for when files are copied into it. I have set the Filter
property to *.* and the NotifyFilter property to CreationTime. However, the
Created event never gets raised. I'm running it from a system service if
that makes any difference.
 
J

Jeff Winn

FileSystemWatcher gets kinda tricky at times. Things you expect to work
often don't, since you never really know how the files are getting
manipulated and often times the events get raised more than you expect them
to. The system service thing wouldn't be a problem (unless you're running it
on Vista, which UAC may be a problem depending which account you're using).
I've done something similar before with Windows services.

I'd try wiring up all the events and filter types and see if it's working at
all. You can always remove the filters and events you don't need later once
you determine that there isn't a problem with it monitoring the folder.

Also, don't forget to set EnableRaisingEvents property to true when you want
it to start monitoring. :blush:)
 
A

Alvin Bruney [ASP.NET MVP]

How do you know it's not working? There maybe an exception which you won't
see since it is a service. A simple test project I created as a service is
working fine. Here is my test case.

using System;
using System.ServiceProcess;
using System.IO;

namespace WindowsService1
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
FileStream fs = new FileStream(@"c:\temp\starting.txt",
FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("i'm starting");
sw.Flush();
sw.Close();
fs.Close();
}

protected override void OnStop()
{
FileStream fs = new FileStream(@"c:\temp\stopping.txt",
FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("i'm stopping");
sw.Flush();
sw.Close();
fs.Close();

}

private void fileSystemWatcher1_Changed(object sender,
System.IO.FileSystemEventArgs e)
{
FileStream fs = new FileStream(@"c:\temp\vapor.txt",
FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("i'm happy");
sw.Flush();
sw.Close();
fs.Close();

}
}
}


--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $19.99
 
W

Waldy

Alvin Bruney said:
How do you know it's not working?

I'm attaching to the service in the debugger, and the event code never gets
executed. I'm copying the file from another folder into the folder that is
being monitored. Is that the issue? Do you have to actually create the
file in the watched folder for the creation event to fire?
 
A

Alvin Bruney [ASP.NET MVP]

yes you do have to create it in the watched folder. If you copy it, it's
just a copy not a create - at least I think that is how it should work. What
worked for me is opening notepad, typing some stuff and saving it in the
directory. That creates a new file with a new file stamp. Copying does not
change the file create timestamp.

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $19.99
 
J

Jeff Winn

Keep this in mind though when you're working with the FileSystemWatcher -
the file you're watching for may end up in the folder after it's been
created by a process that creates a temp file and then renames the file once
it's been saved. Also, the events may be raised multiple times for the same
file depending how the editor used manipulated it.

I was monitoring a file for changes in the past and had no issues when I was
using notepad to edit it. As soon as I edited the file in Visual Studio the
events weren't being raised like I had expected.

Alvin Bruney said:
yes you do have to create it in the watched folder. If you copy it, it's
just a copy not a create - at least I think that is how it should work.
What worked for me is opening notepad, typing some stuff and saving it in
the directory. That creates a new file with a new file stamp. Copying does
not change the file create timestamp.

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $19.99
-------------------------------------------------------


Waldy said:
I'm attaching to the service in the debugger, and the event code never
gets executed. I'm copying the file from another folder into the folder
that is being monitored. Is that the issue? Do you have to actually
create the file in the watched folder for the creation event to fire?
 

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