?
Solved

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

Posted on 2010-04-06
6
Medium Priority
?
807 Views
Last Modified: 2013-11-27
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
        }
    }
}
0
Comment
Question by:snowdog_2112
6 Comments
 
LVL 11

Expert Comment

by:Mihai Stancescu
ID: 29986853
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
 

Author Comment

by:snowdog_2112
ID: 30023809
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 30044327
Since this is a Windows Service, are you logging any exceptions?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 11

Expert Comment

by:Mihai Stancescu
ID: 30051801
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
 

Accepted Solution

by:
snowdog_2112 earned 0 total points
ID: 30144275
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
 
LVL 2

Expert Comment

by:cefranklin
ID: 34236951
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question