[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

msnews.microsoft.com

6/23/2008 3:19:00 PM

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.


5 Answers

Jeff Winn

6/23/2008 11:46:00 PM

0

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. :o)

"Waldy" <someone@microsoft.com> wrote in message
news:OewaISU1IHA.3884@TK2MSFTNGP05.phx.gbl...
> 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.
>

Alvin Bruney [ASP.NET MVP]

6/24/2008 12:17:00 AM

0

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
-------------------------------------------------------


"Waldy" <someone@microsoft.com> wrote in message
news:OewaISU1IHA.3884@TK2MSFTNGP05.phx.gbl...
> 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.
>

msnews.microsoft.com

6/24/2008 8:27:00 AM

0

"Alvin Bruney [ASP.NET MVP]" <vapor dan using hot male spam filter> wrote in
message news:4F74C663-9777-4176-AF5D-BAD8B4B79DA2@microsoft.com...
> 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?


Alvin Bruney [ASP.NET MVP]

6/26/2008 2:16:00 AM

0

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" <someone@microsoft.com> wrote in message
news:uxGLfQd1IHA.2208@TK2MSFTNGP04.phx.gbl...
> "Alvin Bruney [ASP.NET MVP]" <vapor dan using hot male spam filter> wrote
> in message news:4F74C663-9777-4176-AF5D-BAD8B4B79DA2@microsoft.com...
>> 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?
>

Jeff Winn

6/26/2008 5:19:00 AM

0

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 [ASP.NET MVP]" <vapor dan using hot male spam filter> wrote in
message news:O3OYQKz1IHA.528@TK2MSFTNGP02.phx.gbl...
> 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" <someone@microsoft.com> wrote in message
> news:uxGLfQd1IHA.2208@TK2MSFTNGP04.phx.gbl...
>> "Alvin Bruney [ASP.NET MVP]" <vapor dan using hot male spam filter> wrote
>> in message news:4F74C663-9777-4176-AF5D-BAD8B4B79DA2@microsoft.com...
>>> 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?
>>