2

I am currently trying to keep a counter on c# on a local file folder for new files that are created.

I have two sub directories to CD and LP that I have to keep checking.

File System Watcher is only keeping track of my copied folders. Basically I need to keep track of folders created starting with EM* but my code shows the counter increasing when I copy and paste folders and not when I create the EM* folders. e.g EM1 EM2 only EM2-copy increases the counter and even then sometimes it increases +2

        static int LPcounter { get; set; }
        static int CDcounter { get; set; }
        static int LPCreated;
        static int CDCreated;
        FileSystemWatcher CDdirWatcher = new FileSystemWatcher();
        FileSystemWatcher LPdirWatcher = new FileSystemWatcher();

        public Form1()
        {
            InitializeComponent();

            while (true) 
                watch();
        }

        public void watch()
        { 

            CDdirWatcher.Path = @"C:\Data\LotData\CD";
            CDdirWatcher.Filter = "EM*";
            CDdirWatcher.NotifyFilter = NotifyFilters.DirectoryName | NotifyFilters.LastWrite;
            CDdirWatcher.EnableRaisingEvents = true;
            CDdirWatcher.Created += CDdirWatcher_Created; 


            LPdirWatcher.Path = @"C:\Data\LotData\LP";
            LPdirWatcher.Filter = "EM*";
            LPdirWatcher.NotifyFilter = NotifyFilters.DirectoryName;
            LPdirWatcher.EnableRaisingEvents = true;
            LPdirWatcher.Created += LPdirWatcher_Created;

        } 
        private static void CDdirWatcher_Created(object sender, FileSystemEventArgs e)
        {
            CDCreated += 1;
        }
        private static void LPdirWatcher_Created(object sender, FileSystemEventArgs e)
        {
            LPCreated += 1;
        }
  • Your requirement is not clear enough – sujith karivelil Nov 04 '19 at 07:21
  • I'm sorry if my requirements is not clear, which part is confusing? Sorry I'm still new to posting on stack. I've edited my post to be more clear btw – Jonathan James Nov 04 '19 at 07:25
  • What I understood is, you want to add a watch for all names which are starting with `EM` and the watcher is doing its job perfectly for `EM2-copy` as well since it also starts with `EM` then whats wrong here? – sujith karivelil Nov 04 '19 at 07:27
  • For some reason CDCreated is not keeping count of anything created such as EM1 , EM2 but when I copy and paste old files while debugging the counter goes up – Jonathan James Nov 04 '19 at 07:29
  • Silly question but why are you calling watch() in an infiinite loop? You should only need to call it once to setup the watchers and then you will receive events from these, I wonder is that part of the problem? It's been a while since I did any Forms development but this doesn't look right... – Stephen Byrne Nov 04 '19 at 09:09
  • Not a silly question @Stephen Byrne actually I'm not sure myself cause I'm trying to keep the method alive. I'm trying a few things with threading now actually – Jonathan James Nov 05 '19 at 01:55
  • @JonathanJames - cool. IIRC, the watcher itself will run on its own thread so you should only need to register the events once, even if that's not the root cause of the issue I would remove the loop. – Stephen Byrne Nov 05 '19 at 11:08

1 Answers1

2

Your code is right, try to create directory with console and MKDIR, it will work. If you create a directory from Explorer its first created as "New folder" and then renamed.

From Microsoft web: copy and paste is interpreteds as rename https://learn.microsoft.com/en-us/dotnet/api/system.io.filesystemwatcher.notifyfilter?view=netframework-4.8

The operating system and FileSystemWatcher object interpret a cut-and-paste action or a move action as a rename action for a folder and its contents

From the same docu, events can raise multipletimes:

Common file system operations might raise more than one event. For example, when a file is moved from one directory to another, several OnChanged and some OnCreated and OnDeleted events might be raised.

Ive
  • 1,321
  • 2
  • 17
  • 25
  • how do I create the correct filter to fire the count event correctly when a new folder is created and not modified? – Jonathan James Nov 04 '19 at 08:19
  • Upvoted :) and @jonathan james: you may additionally see this answer https://stackoverflow.com/a/1765094/3774666 for file system event fired twice for the which might be causing +2. – Kryptonian Nov 04 '19 at 08:21