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
        }
    }
}
snowdog_2112Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mihai StancescuSoftware Engineer Commented:
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
0
snowdog_2112Author Commented:
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?
0
Bob LearnedCommented:
Since this is a Windows Service, are you logging any exceptions?
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Mihai StancescuSoftware Engineer Commented:
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
0
snowdog_2112Author Commented:
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);
 
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cefranklinCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.