title: File access error with FileSystemWatcher when multiple files are added to a directory
body: <p>I am running into an issue with a FileSystemWatcher when multiple files are placed into the watched directory.  I want to parse the file as soon as it is placed in the directory.  Typically, the first file parses fine, but adding a second file to the directory causes an access issue.  Occasionally, the first file doesn't even parse.  There is only one application running and watching this directory.  Eventually, this process will be running on multiple machines and they will be watching a shared directory but only one server can parse each file as the data is imported into a database and there are no primary keys.</p>
  
  <p>Here is the FileSystemWatcher code:</p>
  
  <pre><code>public void Run() {
    FileSystemWatcher watcher = new FileSystemWatcher("C:\\temp");
    watcher.NotifyFilter = NotifyFilters.FileName;
    watcher.Filter = "*.txt";
  
    watcher.Created += new FileSystemEventHandler(OnChanged);
  
    watcher.EnableRaisingEvents = true;
    System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
  }
  </code></pre>
  
  <p>Then the method that parses the file:</p>
  
  <pre><code>private void OnChanged(object source, FileSystemEventArgs e) {
    string line = null;
  
    try {
      using (FileStream fs = new FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.None)) {
        using (StreamReader sr = new StreamReader(fs)) {
          while (sr.EndOfStream == false) {
            line = sr.ReadLine();
            //parse the line and insert into the database
          }
        }
      }
    }
    catch (IOException ioe) {
      Console.WriteLine("OnChanged: Caught Exception reading file [{0}]", ioe.ToString());
    }
  </code></pre>
  
  <p>When moving the second file, it is catching </p>
  
  <blockquote>
    <p>System.IO.IOException: The process cannot access the file 'C:\Temp\TestFile.txt' because it is being used by another process.</p>
  </blockquote>
  
  <p>I would expect to see this error if it was running on multiple machines, but it is only running on one server for now.  There shouldn't be another process using this file - I have them created and copy them into the directory when the application is running.</p>
  
  <p>Is this the proper way to set up the FileSystemWatcher?  How can I see what has the lock on this file?  Why doesn't it parse both files - do I have to close the FileStream?  I want to keep the FileShare.None option because I only want one server to parse the file - the server that gets to the file first parses it.</p>