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
- static void watch()
- {
-
- FileSystemWatcher watcher = new FileSystemWatcher();
- watcher.Path = @"Q:\New_Folder";
-
- watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
-
-
- watcher.Filter = "*.DAT";
- watcher.InternalBufferSize = 64000;
-
- watcher.Created += new FileSystemEventHandler(watcher_Created);
- watcher.Changed += new FileSystemEventHandler(OnChanged);
-
-
- watcher.EnableRaisingEvents = true;
-
-
- Console.WriteLine("Watching Directory for New Files. To break the process Press \'q\'");
- while(Console.Read()!='q');
-
- }
-
- static void watcher_Created(object source, FileSystemEventArgs e)
- {
-
-
- Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
- SomeGlobalVariables.queuenames.Enqueue(e.FullPath);
-
-
-
- foreach (string number in SomeGlobalVariables.queuenames)
- {
- Console.WriteLine(number);
- }
-
- foreach (var filenames in SomeGlobalVariables.queuenames)
- {
-
-
- var fileName = filenames;
- var Name_File = Path.GetFileName(fileName).Replace(".DAT","").Remove(0,6);
- var tempfilepath = (@"C:\Users\" + Name_File + ".CSV");
- Console.WriteLine(fileName);
-
- ReadData(tempfilepath, fileName);
-
-
- }
-
- }
-
-
- static void OnChanged(object source, FileSystemEventArgs e)
- {
-
-
-
-
- if (!SomeGlobalVariables.queuenames.Contains(e.FullPath))
- {
- Console.WriteLine("The file is not in the list");
- Console.ReadKey();
- }
- else if (SomeGlobalVariables.queuenames.Contains(e.FullPath))
- {
- var last_item = SomeGlobalVariables.queuenames.Last();
- SomeGlobalVariables.filenameslist.Remove(last_item);
- Console.WriteLine("deleted");
-
- foreach (string number in SomeGlobalVariables.queuenames)
- {
- Console.WriteLine(number);
- }
-
-
- }
- }