title: Is there any alternative for FileSystemWatcher in .NET C#?
body: <p>I am new to .NET Core and currently working on a use case where I need to process the files in a directory.</p>
  <p>The application is using the <code>FileSystemWatcher</code> class to monitor the directory(s).
  <br>Below is the code that I am using to monitor the directories.</p>
  <pre><code>
  private FileSystemWatcher watcher = new FileSystemWatcher();
  private List&lt;CustomFileEvent&gt; custEventsList = new List();
  
  private void MonitorDirectory(strinig dirName) 
  {
      
      watcher.Path = dirName;
      watcher.IncludeSubdirectories = true;
      
      watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.CreationTime | NotifyFilters.LastAccess | NotifyFilters.DirectoryName;
      
      watcher.Filter = &quot;*.*&quot;;
      
      // Add created event handler
      watcher.Created += new FileSystemEventHandler(OnChanged);
      //
      //watcher.Deleted += new FileSystemEventHandler(OnChanged);
      watcher.Renamed += new RenamedEventHandler(OnRenamed);
  
      // Setting the internal buffer size to 24kb. By default it is 8kb
      watcher.InternalBufferSize = 24000;
  
      // Begin watching.
      watcher.EnableRaisingEvents = true;
      
      watcher.WaitForChanged(WatcherChangeTypes.All);
  }
  
  private void OnChanged(object source, FileSystemEventArgs e)
  {
      Console.WriteLine(&quot;Event Occured&quot;);
      Console.WriteLine($&quot;File: {e.FullPath} {e.ChangeType}&quot;);
      
      // Code to handle the file created event
      // Passing the parameters fileName, fullPath, dateTime, and size of the file
      CreateFileEvent(......)
      
  }
  
  private void CreateFileEvent(string fileName, string fullPath, DateTime timeStamp,long size) {
      // Code to create the File created event
      var customFileEvent = new CustomFileEvent()
      {
          FileName = fileName,
          FullPath = fullPath,
          TimeStamp = timeStamp,
          Size = size
      };
      
      custEventsList.Add(customFileEvent);
  }
  
  private void TimerFunction(object sender, ElapsedEventArgs e)
  {
      if (custEventsList != null &amp;&amp; custEventsList.Count != 0)
      {
          Console.WriteLine(&quot;Total events :: &quot; + custEventsList .Count);
  
          foreach (var evnt in custEventsList)
          {
              ProcessEvent( ..... arguments to send the file watcher events);
          }
      }
      
      // Clear your file List.
      custEventsList = new List&lt;CustomFileEvent&gt;();  // CustomFileEvent is a custom class
  }
  </code></pre>
  <p>Assuming the application is monitoring the folder C:\MyAppFiles\AppEvents<br />
  The TimerFunction is created to handle the events after each 20 secs interval.
  The <code>ProcessEvent</code> method is actually used to send the file details to another Windows Service which processes the file and delete it.</p>
  <p>With this code I am successfully able to process one file at a time and delete it. The problem occurs when I am trying to process multiple files using below steps.</p>
  <ol>
  <li>Copy a FileData.txt to watching folder. The <code>Created</code> event occurs and OnChanged function get called.</li>
  <li>The <code>CreateFileEvent</code> creates the custom event.</li>
  <li>TimerFunction handle the events after each 20 secs.</li>
  <li>Copy FileData_2.txt before 20 secs.</li>
  <li>The folder is having 2 files for processing.</li>
  <li>The code processes both the files and delete the files. The folder is empty now.</li>
  <li>Now I placed another file FileData_3.txt</li>
  <li>Found that <code>Created</code> event is not getting generated. And the file is not getting processed.</li>
  </ol>
  <p>UPDATE: I observed that after processing the the file in ProcessEvent function, the FileSystemWatcher is getting disposed automatically. This happens only in case when I am trying to put multiple files for processing. In case of single file it works successfully.</p>
  <p>Please advice.</p>