Link to home
Start Free TrialLog in
Avatar of snowdog_2112
snowdog_2112Flag for United States of America

asked on

URGENT! visual studio express 2008 C# filesystemwatcher - not triggering

I have a Window Service with a System.Net.FileSystemWatcher 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.FileSystemWatcher();
            ((System.ComponentModel.ISupportInitialize)(this.FSWatcherTest)).BeginInit();
            //
            // FSWatcherTest
            //
            this.FSWatcherTest.EnableRaisingEvents = true;
            this.FSWatcherTest.IncludeSubdirectories = 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.NotifyFilter = ((System.IO.NotifyFilters)((((((((System.IO.NotifyFilters.FileName | System.IO.NotifyFilters.DirectoryName)
                        | System.IO.NotifyFilters.Attributes)
                        | System.IO.NotifyFilters.Size)
                        | System.IO.NotifyFilters.LastWrite)
                        | System.IO.NotifyFilters.LastAccess)
                        | System.IO.NotifyFilters.CreationTime)
                        | System.IO.NotifyFilters.Security)));

            // <dk> filter .xml files only
            this.FSWatcherTest.Filter = "*.*";
            this.FSWatcherTest.Path = "C:\\inetpub\\wwwroot\\OutDir\\";
            // </dk>
            //
            // Service1
            //
            this.ServiceName = "TestCSWinWatcherService";
            ((System.ComponentModel.ISupportInitialize)(this.FSWatcherTest)).EndInit();

Here is code for the events:

        private System.IO.FileSystemWatcher FSWatcherTest;

        /* DEFINE WATCHER EVENTS... */

        /// <summary>
        /// Event occurs when the contents of a File or Directory is changed
        /// </summary>
        private void FSWatcherTest_Changed(object sender, System.IO.FileSystemEventArgs e)
        {
            //code here for newly changed file or directory

            FileStream fs = new FileStream(@"c:\inetpub\wwwroot\xml\changed.txt",
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.FullPath, "C:\\inetpub\\wwwroot\\xml" + e.Name);
         
       
        }

        /// <summary>
        /// Event occurs when the a File or Directory is created
        /// </summary>
        private void FSWatcherTest_Created(object sender, System.IO.FileSystemEventArgs e)
        {
            //code here for newly created file or directory
            // <dk> read the file

            FileStream fs = new FileStream(@"c:\inetpub\wwwroot\xml\created.txt",
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.FullPath, "C:\\inetpub\\wwwroot\\xml" + e.Name);
         
           
        }

        /// <summary>
        /// Event occurs when the a File or Directory is deleted
        /// </summary>
        private void FSWatcherTest_Deleted(object sender, System.IO.FileSystemEventArgs 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(object sender, System.IO.RenamedEventArgs e)
        {
            //code here for newly renamed file or directory
        }
    }
}
Avatar of Mihai Stancescu
Mihai Stancescu
Flag of Romania image

1. At this line : "  // <dk> filter .xml files only
            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
Avatar of snowdog_2112

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?
Avatar of Bob Learned
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
ASKER CERTIFIED SOLUTION
Avatar of snowdog_2112
snowdog_2112
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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(this.FileMonitor_Changed);
                  this.FileMonitor.Renamed += new System.IO.RenamedEventHandler(this.FileMonitor_OnRenamed);
                  this.FileMonitor.Changed += new System.IO.FileSystemEventHandler(this.FileMonitor_Changed);
                  this.FileMonitor.Created += new System.IO.FileSystemEventHandler(this.FileMonitor_Changed);
 

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.