-1

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.

Maybe some pseudo code might help

        static int CDcounter = 0;
        static int LPcounter = 0;
        public void returnFileCount()
        {
            //Location of Files
            string[] files = Directory.GetFiles(@"C:\Data\LotData");


            //if file sub directory = cd go in and check for new files
            //if new files pop up in cd CDcounter ++ same for LP
        }

2 Answers2

0

I managed to find the answer guys

//Counter for directories
CDcounter = new System.IO.DirectoryInfo(@"C:\Data\LotData\CD").GetDirectories().Length;

https://stackoverflow.com/a/5996574/7144746

0

You could use Directory.GetDirectories to get the initial count of directories.

CDcounter = Directory.GetDirectories(@"C:\Data\LotData\CD").Length

Then you can use a FileSystemWatcher and filter only for directories.

        dirWatcher = new FileSystemWatcher();
        dirWatcher.Path = @"C:\Data\LotData\CD";
        dirWatcher.NotifyFilter = NotifyFilters.DirectoryName;
        dirWatcher.EnableRaisingEvents = true;

You can subscribe to the Created and Deleted events to keep a count of directories.

This answer details how to use FileSystemWatcher for directories and files

James
  • 41
  • 4