title: The process cannot access the file because it is being used by another process at deletion
body: <p>I thought I solved this problem by closing and disposing my reader, but still in some cases the file was in use. Next I called the Garbage Collector, so the file will be released.
  This solves 99% of all the issues which will bring up this error.
  Code used:</p>
  
  <pre><code>        public override void Finish()
          {
              // Kill the reader!
              if (_reader != null)
              {
                  _reader.Close();
                  _reader.Dispose();
  
                  // Make sure the server doesn't hold the file
                  GC.Collect();
              }
              DeleteFile();
          }
  </code></pre>
  
  <p>Finish is called after a big process which will handle the file's content.</p>
  
  <p>When I process a file with only 1 (or very few) line I <strong>sometimes</strong> get this error. It seems windows is to quick and <code>DeleteFile();</code> fails.
  It is very hard for me to reproduce this error, but sometimes it just happens twice in a row.<br>
  This never occurs when I process a file with takes more then 2 seconds to process.<br>
  I cannot use a using because files can be GB's, and windows doesn't like it when its memory is getting too full. Also, this way the process performs way better.</p>
  
  <p><strong>Question:</strong><br>
  Is there anything else I can do to prevent this error?</p>
  
  <p>PS: Feel free to ask for more information.</p>
  
  <p>EDIT:<br>
  Code to delete a file</p>
  
  <pre><code>        protected void DeleteFile()
          {
              // Delete the file
              if (FileName != null &amp;&amp; File.Exists(FileName))
                  File.Delete(FileName);
          }
  </code></pre>
  
  <p>Code to create the file</p>
  
  <pre><code>        protected void WriteFile()
          {
              // Prepare variables
              string path = Path.GetTempPath();
  
              path += "\\MyTempFile";
  
              // Modifiy path to avoid overwriting an existing file.
  
              path += ".csv";
  
              // Write the file to the temp folder
              using (FileStream fs = new FileStream(path, FileMode.Create))
              {
                  fs.Write(MyFile, 0, MyFile.Length);
              }
  
              // Was the writing successfully completed?
              _FileName = File.Exists(path) ? path : null;
          }
  </code></pre>
  
  <p>Code to create the reader</p>
  
  <pre><code>        protected override void ReadFile()
          {
              if (FileName == null)
                  WriteFile();
  
              // Read the lines
              _reader = new StreamReader(FileName, Encoding.Default, true);
              while (_reader.Peek() != -1)
              {
                  TotalRows++;
                  _reader.ReadLine();
              }
  
              // Read the lines
              _reader = new StreamReader(FileName, Encoding.Default, true);
          }
  </code></pre>
  
  <p>I use an abstract class to determine how the input should be read.
  With the following statement I'll loop through the content of the file.</p>
  
  <pre><code> while (FileReader.NextRow(out currentRow, out currentRowNumber))
          {
                 // Process current Row...
          }
  </code></pre>
  
  <p>The method NextRow() Looks like this</p>
  
  <pre><code>        public override bool NextRow(out List&lt;object&gt; nextRow, out int rowNumber)
          {
              if (RowNumber &gt; TotalRows)
              {
                  nextRow = null;
                  rowNumber = 0;
  
                  return false;
              }
  
              // Set the row number to return
              rowNumber = RowNumber;
  
              // Prepare the row
              nextRow = _reader.ReadLine().ExtensionThatProcessesTheRow();
              RowNumber++;
  
              return true;
          }
  </code></pre>
  
  <p>After the while loop closes I call the finish process. <code>FileReader.Finish();</code></p>