snowdog_2112
asked on
URGENT! visual studio express 2008 C# filesystemwatcher - not triggering
I have a Window Service with a System.Net.FileSystemWatch er configured to watch a particular folder.
When the service starts, it creates a text file (basically saying the service started). When I do anything in the folder being watched, it's supposed to copy the affected file to another location and create a separate text file (just debugging at this point).
The problem is, nothing happens when the watched directory is changed - files created, deleted, modified - none of them seem to trigger the events.
Here is the initialize code:
private void InitializeComponent()
{
this.FSWatcherTest = new System.IO.FileSystemWatche r();
((System.ComponentModel.IS upportInit ialize)(th is.FSWatch erTest)).B eginInit() ;
//
// FSWatcherTest
//
this.FSWatcherTest.EnableR aisingEven ts = true;
this.FSWatcherTest.Include Subdirecto ries = true;
//<dk> - note: only raise event if one of the notifiers is
//triggered, e.g. size or lastwrite changes - do not need filename. </dk>
this.FSWatcherTest.NotifyF ilter = ((System.IO.NotifyFilters) ((((((((Sy stem.IO.No tifyFilter s.FileName | System.IO.NotifyFilters.Di rectoryNam e)
| System.IO.NotifyFilters.At tributes)
| System.IO.NotifyFilters.Si ze)
| System.IO.NotifyFilters.La stWrite)
| System.IO.NotifyFilters.La stAccess)
| System.IO.NotifyFilters.Cr eationTime )
| System.IO.NotifyFilters.Se curity)));
// <dk> filter .xml files only
this.FSWatcherTest.Filter = "*.*";
this.FSWatcherTest.Path = "C:\\inetpub\\wwwroot\\Out Dir\\";
// </dk>
//
// Service1
//
this.ServiceName = "TestCSWinWatcherService";
((System.ComponentModel.IS upportInit ialize)(th is.FSWatch erTest)).E ndInit();
Here is code for the events:
private System.IO.FileSystemWatche r FSWatcherTest;
/* DEFINE WATCHER EVENTS... */
/// <summary>
/// Event occurs when the contents of a File or Directory is changed
/// </summary>
private void FSWatcherTest_Changed(obje ct sender, System.IO.FileSystemEventA rgs e)
{
//code here for newly changed file or directory
FileStream fs = new FileStream(@"c:\inetpub\ww wroot\xml\ changed.tx t",
FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream. Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine(" changed \n");
m_streamWriter.Flush();
m_streamWriter.Close();
m_streamWriter = null;
fs = null;
System.IO.File.Copy(e.Full Path, "C:\\inetpub\\wwwroot\\xml " + e.Name);
}
/// <summary>
/// Event occurs when the a File or Directory is created
/// </summary>
private void FSWatcherTest_Created(obje ct sender, System.IO.FileSystemEventA rgs e)
{
//code here for newly created file or directory
// <dk> read the file
FileStream fs = new FileStream(@"c:\inetpub\ww wroot\xml\ created.tx t",
FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream. Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine(" created \n");
m_streamWriter.Flush();
m_streamWriter.Close();
m_streamWriter = null;
fs = null;
System.IO.File.Copy(e.Full Path, "C:\\inetpub\\wwwroot\\xml " + e.Name);
}
/// <summary>
/// Event occurs when the a File or Directory is deleted
/// </summary>
private void FSWatcherTest_Deleted(obje ct sender, System.IO.FileSystemEventA rgs e)
{
//code here for newly deleted file or directory
}
/// <summary>
/// Event occurs when the a File or Directory is renamed
/// </summary>
private void FSWatcherTest_Renamed(obje ct sender, System.IO.RenamedEventArgs e)
{
//code here for newly renamed file or directory
}
}
}
When the service starts, it creates a text file (basically saying the service started). When I do anything in the folder being watched, it's supposed to copy the affected file to another location and create a separate text file (just debugging at this point).
The problem is, nothing happens when the watched directory is changed - files created, deleted, modified - none of them seem to trigger the events.
Here is the initialize code:
private void InitializeComponent()
{
this.FSWatcherTest = new System.IO.FileSystemWatche
((System.ComponentModel.IS
//
// FSWatcherTest
//
this.FSWatcherTest.EnableR
this.FSWatcherTest.Include
//<dk> - note: only raise event if one of the notifiers is
//triggered, e.g. size or lastwrite changes - do not need filename. </dk>
this.FSWatcherTest.NotifyF
| System.IO.NotifyFilters.At
| System.IO.NotifyFilters.Si
| System.IO.NotifyFilters.La
| System.IO.NotifyFilters.La
| System.IO.NotifyFilters.Cr
| System.IO.NotifyFilters.Se
// <dk> filter .xml files only
this.FSWatcherTest.Filter = "*.*";
this.FSWatcherTest.Path = "C:\\inetpub\\wwwroot\\Out
// </dk>
//
// Service1
//
this.ServiceName = "TestCSWinWatcherService";
((System.ComponentModel.IS
Here is code for the events:
private System.IO.FileSystemWatche
/* DEFINE WATCHER EVENTS... */
/// <summary>
/// Event occurs when the contents of a File or Directory is changed
/// </summary>
private void FSWatcherTest_Changed(obje
{
//code here for newly changed file or directory
FileStream fs = new FileStream(@"c:\inetpub\ww
FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.
m_streamWriter.WriteLine("
m_streamWriter.Flush();
m_streamWriter.Close();
m_streamWriter = null;
fs = null;
System.IO.File.Copy(e.Full
}
/// <summary>
/// Event occurs when the a File or Directory is created
/// </summary>
private void FSWatcherTest_Created(obje
{
//code here for newly created file or directory
// <dk> read the file
FileStream fs = new FileStream(@"c:\inetpub\ww
FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.
m_streamWriter.WriteLine("
m_streamWriter.Flush();
m_streamWriter.Close();
m_streamWriter = null;
fs = null;
System.IO.File.Copy(e.Full
}
/// <summary>
/// Event occurs when the a File or Directory is deleted
/// </summary>
private void FSWatcherTest_Deleted(obje
{
//code here for newly deleted file or directory
}
/// <summary>
/// Event occurs when the a File or Directory is renamed
/// </summary>
private void FSWatcherTest_Renamed(obje
{
//code here for newly renamed file or directory
}
}
}
ASKER
I changed it to monitor all files as a test to see if that was an issue.
I added Everyone with modify access to the watched folder.
I'd rather not have to build/test/debug another app and still have to come back to this windows service. It's a pain as it is to have to uninstall the service any time I change the code.
Side question - can I just stop the service rebuild the .exe and restart the service if the .exe is in the same location when I make a change? Or is it better (like I have been doing) to unisntall the Windows Service, rebuild the app in VS, and then reinstall the service?
I added Everyone with modify access to the watched folder.
I'd rather not have to build/test/debug another app and still have to come back to this windows service. It's a pain as it is to have to uninstall the service any time I change the code.
Side question - can I just stop the service rebuild the .exe and restart the service if the .exe is in the same location when I make a change? Or is it better (like I have been doing) to unisntall the Windows Service, rebuild the app in VS, and then reinstall the service?
Since this is a Windows Service, are you logging any exceptions?
Yeah you can stop and start the service again without uninstall.
C:\\inetpub\\wwwroot\\ it's a special folder owned by IIS, try setting other path and see if you have the same results.
As a tip : you can try setting the path into an .ini file and you just have to restart the service and you have the new path set.
Hope this helps!
Regards,
Mishu
C:\\inetpub\\wwwroot\\ it's a special folder owned by IIS, try setting other path and see if you have the same results.
As a tip : you can try setting the path into an .ini file and you just have to restart the service and you have the new path set.
Hope this helps!
Regards,
Mishu
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
snowdog_2112:I appreciate your time, but I was not talking about simply restarting the service. I was asking if I had to uninstall the service any time I rebuilt the project.
Specifically, as I'm debugging the app, I am rebuilding the project several times per hour, and it's a pain to keep stopping the service, uninstall the service, build, build installer, run installer, start service, test, repeat...
I have rewritten the code and now it works. I have no idea what was wrong with this code other than in the new code, the events are "defined" by using the following:
(note: FSWatcherTest from above is now FileMonitor)
this.FileMonitor.Deleted += new System.IO.FileSystemEventHandler(thi s.FileMoni tor_Change d);
this.FileMonitor.Renamed += new System.IO.RenamedEventHandler(this.F ileMonitor _OnRenamed );
this.FileMonitor.Changed += new System.IO.FileSystemEventHandler(thi s.FileMoni tor_Change d);
this.FileMonitor.Created += new System.IO.FileSystemEventHandler(thi s.FileMoni tor_Change d);
Well, I would hope that you had your event handlers defined ahead of time. Now that I see it, would make sense that it does nothing if there weren't any triggers to run when something happened.
this.FSWatcherTest.Filter = "*.*";" your not filtering anything; to filter only xml files yout must write like this: "*.xml";
2. Does the service has access to the watched folder?
3. Try only a few notify filter and see how that works, and also try putting it in a Windows Form app.
Hope this helps!
Regards,
Mishu