|
Prev: webbrowser control
Next: ASP.Net Hosting
From: Waldy on 23 Jun 2008 11:19 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.
From: Jeff Winn on 23 Jun 2008 19:46 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(a)microsoft.com> wrote in message news:OewaISU1IHA.3884(a)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. >
From: "Alvin Bruney [ASP.NET MVP]" vapor dan using hot male spam on 23 Jun 2008 20:17 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(a)microsoft.com> wrote in message news:OewaISU1IHA.3884(a)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. >
From: Waldy on 24 Jun 2008 04:27 "Alvin Bruney [ASP.NET MVP]" <vapor dan using hot male spam filter> wrote in message news:4F74C663-9777-4176-AF5D-BAD8B4B79DA2(a)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?
From: "Alvin Bruney [ASP.NET MVP]" vapor dan using hot male spam on 25 Jun 2008 22:15
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(a)microsoft.com> wrote in message news:uxGLfQd1IHA.2208(a)TK2MSFTNGP04.phx.gbl... > "Alvin Bruney [ASP.NET MVP]" <vapor dan using hot male spam filter> wrote > in message news:4F74C663-9777-4176-AF5D-BAD8B4B79DA2(a)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? > |