Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Windows service
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.
 
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 25-Jun-13 20:49pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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());
            }
        }
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

you can use my tool
Smart Watcher[^]
just develop a plugin to move files
  Permalink  
Comments
Member 9711577 at 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
0 OriginalGriff 515
1 Maciej Los 340
2 Richard MacCutchan 265
3 BillWoodruff 225
4 Mathew Soji 155
0 OriginalGriff 8,804
1 Sergey Alexandrovich Kryukov 7,457
2 DamithSL 5,689
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411028.1 | Last Updated 27 Jun 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100