title: FileSystemWatcher to monitor directory size
body: <p>Hi I am creating a windows service to watch certain directories to see if the size of the directory is reaching its limit.
  I have created a file system watcher as follows:</p>
  <pre><code>            FileSystemWatcher watcher = new FileSystemWatcher();
              watcher.Path = dirPaths[i].ToString();
              watcher.NotifyFilter = NotifyFilters.Size;
              watcher.EnableRaisingEvents = true;
              watcher.Changed += new FileSystemEventHandler(OnChanged);
  </code></pre>
  <p>and</p>
  <pre><code> private void OnChanged(object source, FileSystemEventArgs e)
      {
          try
          {
          
          string directory = new DirectoryInfo(e.FullPath).Parent.FullName;//gettting the directory path from the full path
  
          float dirSize = CalculateFolderSize(directory);
  
          float limitSize = int.Parse(_config.TargetSize);//getting the limit size 
  
          if (dirSize &gt; limitSize)
          {
              eventLogCheck.WriteEntry(&quot;the following path has crossed the limit &quot; + directory);
              //TODO: mail sending
          }
      }
      catch (Exception ex)
      {
          eventLogCheck.WriteEntry(ex.ToString());
      }
  
  }
  </code></pre>
  <p><code>CalculateFolderSize</code> checks the size of all the files and subdirectories in the drive.</p>
  <p>Now this works fine when I add a file to the directory e.g. a .xls, .txt, etc. file but if I add a folder to the directory it does not trigger the <code>OnChanged</code> event??</p>
  <p>if I enable:</p>
  <pre><code>watcher.IncludeSubdirectories = true;
  </code></pre>
  <p>it does trigger the <code>Onchanged</code> event but in this case it only checks the subdirectory and not the entire directory.</p>
  <p>Please can someone tell me how I can get this to work such that when I copy a folder to the directory being watched it triggers the <code>Onchanged</code> event and calculates the new size of the directory.</p>
  <p>my <code>CalculateFolderSize</code> function is as follows if this helps:</p>
  <pre><code>//function to calculate the size of the given path
          private float CalculateFolderSize(string folder)
          {
              float folderSize = 0.0f;
              try
              {
                  //Checks if the path is valid or not         
                  if (!Directory.Exists(folder))
                  {
                      return folderSize;
                  }
                  else
                  {
                      try
                      {
                          foreach (string file in Directory.GetFiles(folder))
                          {
                              if (File.Exists(file))
                              {
                                  FileInfo finfo = new FileInfo(file);
                                  folderSize += finfo.Length;
                              }
                          }
                          foreach (string dir in Directory.GetDirectories(folder))
                          {
                              folderSize += CalculateFolderSize(dir);
                          }
                      }
                      catch (NotSupportedException ex)
                      {
                          eventLogCheck.WriteEntry(ex.ToString());
                      }
                  }
              }
              catch (UnauthorizedAccessException ex)
              {
                  eventLogCheck.WriteEntry(ex.ToString());
              }
              return folderSize;
          }
  </code></pre>