Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have Windows Service Which Has A following Tasks To Perform.

[1] Continuously Monitor A Directory And Upon Arrival Of Any New Files Move The File To Some Other Directory.

For The Above Task I Am Using FileSystemWatcher Component.However The Component Works Fine With No Issues When It Is Console Application Or Windows Form,However It Does Not Work When I Convert It To Windows Service.Not Sure What Is The Cause I Do Have All The Local Admin Rights To Run The Windows Service Locally. Here The Code Snippet Which I Am Trying To Execute Through Service.

C#
protected override void OnStart(string[] args)
        {
            thrMain = new Thread(StartWatching);
            thrMain.Start();
        }

        public void StartWatching()
        {
             Logger Logger = new Logger();
            try
            {

                while (true)
                {

                    if (!Directory.Exists(sourceDir))
                        Directory.CreateDirectory(sourceDir);
                    if (!Directory.Exists(processDir))
                        Directory.CreateDirectory(processDir);

                    //Watching the source folder
                    using (FileSystemWatcher fsw = new FileSystemWatcher(sourceDir))
                    {

                        //Call fsw_Created when a created event occurs

                        fsw.Created += new FileSystemEventHandler(fsw_Created);
                        fsw.IncludeSubdirectories = true;
                        fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
                                           | NotifyFilters.FileName | NotifyFilters.DirectoryName;
                        fsw.EnableRaisingEvents = true;

                    }

                    Thread.Sleep(5000);
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Onstart", "Exception in Onstart method. Error : " + ex.ToString());
            }


Not Sure If Filewatcher Has Issues With Specific Events Like Changed/Created Etc...
Posted

Problem is that you are creating new instance of FileSystemWatcher inside loop every time and just after creation as soon as using block ends the instance of Filesystem Watcher get disposed and your thread go to sleep for 5 seconds.

Please create FileSystemWatcher instance outside of while loop and test.

Below is updated code, you can try
public static  void StartWatching()
        {
            Logger Logger = new Logger();
            try
            {
                FileSystemWatcher fsw = new FileSystemWatcher(sourceDir);

                    if (!Directory.Exists(sourceDir))
                        Directory.CreateDirectory(sourceDir);
                    if (!Directory.Exists(processDir))
                        Directory.CreateDirectory(processDir);
                        //Call fsw_Created when a created event occurs

                        fsw.Created += new FileSystemEventHandler(fsw_Created);
                        fsw.IncludeSubdirectories = true;
                        fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
                                           | NotifyFilters.FileName | NotifyFilters.DirectoryName;
                        fsw.EnableRaisingEvents = true;
                        ManualResetEvent mResetEvent = new ManualResetEvent(false);
                        mResetEvent.WaitOne();
                    
            }
            catch (Exception ex)
            {
               Logger.Error("Onstart", "Exception in Onstart method. Error : " + ex.ToString());
            }
        }
 
Share this answer
 
v2
you can use my tool
Smart Watcher[^]
just develop a plugin to move files
 
Share this answer
 
Comments
Member 9711577 4-Apr-14 16:00pm    
FileSystemWatcher file changed event DOES NOT fire when .Net's File.Copy(s,d) service is used to overwrite files. Using a file-stream directly wrapped in using-statement DOES cause the event to fire. It appears Close/flush the is submitting file change event to NTFS.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900