Hesham Hassanein

Hesham Hassanein

  • NA
  • 140
  • 16k

File System Watcher does not fire when more than one file

Oct 11 2016 7:53 AM
Hello,
I have a problem with File System Watcher, It does not fire when i create more than 1 file in the directory i am watching. It only works for 1 file, When 2 or more are copied into the directory at the same time. It does not work. It only sees 1 file.
 
I am doing 2 event handlers. 1 is onCreated and 1 is onChanged, In the Oncreated i add the files to a queue and call the function which does the process of the collected files and in OnChanged i delete one of the files in the list if it exists, If it does not exist, then do nothing. 
 
What can be the problem in this code. Do i have to work with threading to make watcher sees this fast change in the directory or what?. Thanks in Advance. Here is the code
 
  1. static void watch()  
  2.        {  
  3.             // Create a new FileSystemWatcher and set its properties.  
  4.        FileSystemWatcher watcher = new FileSystemWatcher();  
  5.        watcher.Path = @"Q:\New_Folder";  
  6.          
  7.        watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;  
  8.   
  9.     
  10.        watcher.Filter = "*.DAT";  
  11.        watcher.InternalBufferSize = 64000;  
  12.      
  13.        watcher.Created += new FileSystemEventHandler(watcher_Created);  
  14.        watcher.Changed += new FileSystemEventHandler(OnChanged);  
  15.   
  16.   
  17.        watcher.EnableRaisingEvents = true;  
  18.   
  19.   
  20.        Console.WriteLine("Watching Directory for New Files. To break the process Press \'q\'");  
  21.        while(Console.Read()!='q');  
  22.          
  23.        }  
  24.   
  25.        static void watcher_Created(object source, FileSystemEventArgs e)  
  26.        {  
  27.              
  28.            // Specify what is done when a file is created  
  29.            Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);  
  30.            SomeGlobalVariables.queuenames.Enqueue(e.FullPath);  
  31.             
  32.       //     SomeGlobalVariables.filenameslist.ForEach(Console.WriteLine);  
  33.   
  34.            foreach (string number in SomeGlobalVariables.queuenames)  
  35.            {  
  36.                Console.WriteLine(number);  
  37.            }  
  38.   
  39.            foreach (var filenames in SomeGlobalVariables.queuenames)  
  40.            {  
  41.                  
  42.                 
  43.                var fileName = filenames;  
  44.                var Name_File = Path.GetFileName(fileName).Replace(".DAT","").Remove(0,6);  
  45.                var tempfilepath = (@"C:\Users\" + Name_File + ".CSV");  
  46.                Console.WriteLine(fileName);  
  47.                  
  48.                ReadData(tempfilepath, fileName);  
  49.       
  50.   
  51.            }  
  52.             
  53.        }  
  54.         
  55.   
  56.        static void OnChanged(object source, FileSystemEventArgs e)  
  57.        {  
  58.   
  59.        // Specify what is done when a file is changed  
  60.          
  61.            //Console.WriteLine("File: " +  e.FullPath + " " + e.ChangeType);        
  62.            if (!SomeGlobalVariables.queuenames.Contains(e.FullPath))  
  63.            {  
  64.                Console.WriteLine("The file is not in the list");  
  65.                Console.ReadKey();  
  66.            }  
  67.            else if (SomeGlobalVariables.queuenames.Contains(e.FullPath))  
  68.            {  
  69.                var last_item = SomeGlobalVariables.queuenames.Last();  
  70.                SomeGlobalVariables.filenameslist.Remove(last_item);  
  71.                Console.WriteLine("deleted");  
  72.   
  73.                foreach (string number in SomeGlobalVariables.queuenames)  
  74.                {  
  75.                    Console.WriteLine(number);  
  76.                }  
  77.   
  78.                  
  79.            }  
  80.        }  
 
 
 

Answers (4)