title: FileStream and a FileSystemWatcher in C#, Weird Issue "process cannot access the file"
body: <p>I have this complicated code-base that is listening for FileCreated events on a certain folder. When the file is Created (which also includes moving a file to that folder), I want to read in that file and do something with it. It works for the first file, but throws an exception after for all other attempts. In Debug-mode (With VisualStudio) the error would be thrown, but if i just click "continue".. it would then work (without an error).</p>
  
  <p>I have posted simplified code, which demonstrates the issue.</p>
  
  <p>For example, you start the application, click the "start" button, and then "create a new text file"</p>
  
  <p>The output is:</p>
  
  <pre><code>Working
  </code></pre>
  
  <p>If you then create a second file in the exact same manner, the output is:</p>
  
  <pre><code>Broken: The process cannot access the file 'C:\TestFolder\New Text Document (2).txt' because it is being used by another process.
  Working, after breaking
  </code></pre>
  
  <p>After looking at my code, you will see that the above set of print-outs implies that first there was an "cannot access the file" exception thrown, but doing the same call in the catch-statement suddenly works.</p>
  
  <p>This makes no sense to me, since the file is clearly not being used by anything else (i just created it).. and it works a second later anyhow....</p>
  
  <h2>Below is my code</h2>
  
  <p>XAML:</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication1.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="MainWindow" &gt;
      &lt;StackPanel&gt;
          &lt;Button Click="Button_Click"  Content="Start"/&gt;
      &lt;/StackPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Code Behind:</p>
  
  <pre><code>using System;
  using System.Diagnostics;
  using System.IO;
  using System.Threading;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Input;
  
  
  namespace WpfApplication1
  { 
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
          }
  
          private void Button_Click(object sender, RoutedEventArgs e)
          {
              test();
          }
  
  
          String Folder = @"C:\TestFolder";
  
          private void test()
          {
              FileSystemWatcher watch = new FileSystemWatcher(Folder);
              watch.Created += new FileSystemEventHandler(FileCreated);
              watch.EnableRaisingEvents = true;
  
              Process.Start(Folder);
          }
  
          private void FileCreated(object sender, FileSystemEventArgs fsEvent)
          {
  
              if (File.Exists(fsEvent.FullPath))
              {
  
                  // Thread.Sleep(1000);// Sleeping for 1 second seems to prevent the error from happening...?
                  // If i am debugging, and pause on exceptions... then it also suddenly works (similar to the Sleep above)
                  try
                  {
  
                      FileStream fs = new FileStream(fsEvent.FullPath, FileMode.Open); 
                      Console.WriteLine("Working");
                      fs.Close();
                  }
                  catch (IOException ex)
                  {
                      Console.WriteLine("Broken: " + ex.Message);
                      try
                      {                        
                          FileStream fs = new FileStream(fsEvent.FullPath, FileMode.Open);
                          Console.WriteLine("Working, after breaking");
                          fs.Close();
  
                      }
                      catch(IOException ex2)
                      {                        
                          FileStream fs = new FileStream(fsEvent.FullPath, FileMode.Open);
                          Console.WriteLine("really broken: " + ex2.Message);
                          fs.Close();
                      }
                  }
  
  
              }
          }
      }
  }
  </code></pre>