title: File Still Locked Despite putting StreamReader in Using
body: <p>My program goes through all files in a folder, reads them, and without altering their information moves them to another location under a different name. However I cannot use the <code>File.Move</code> method because I get the following <code>IOException</code>:</p>
  
  <blockquote>
    <p>The process cannot access the file because it is being used by another
    process.</p>
  </blockquote>
  
  <p>This is how I am reading the file and adding all its lines to a <code>List&lt;string&gt;</code>:</p>
  
  <pre><code>List&lt;string&gt; lines = null;
  using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
      using (var sr = new StreamReader(fs, Encoding.Default))
      {
          lines = new List&lt;string&gt;();
          while (!sr.EndOfStream)
              lines.Add(sr.ReadLine());
      }
  </code></pre>
  
  <p>And this is the function with which I move the file:</p>
  
  <pre><code>public static bool ArchiveFile(string filePath, string archiveFolderLocation)
  {
      if (!Directory.Exists(archiveFolderLocation))
          Directory.CreateDirectory(archiveFolderLocation);
      try
      {
          string timestamp = string.Format("{0:yyyy-MM-dd HHmmss}", DateTime.Now);
          string newFileName = Path.GetFileNameWithoutExtension(filePath) + " " + timestamp;
          string destination = string.Format("{0}\\{1}{2}", archiveFolderLocation, newFileName, Path.GetExtension(filePath));
          File.Move(filePath, destination);
          return true;
      }
      catch (Exception ex)
      {
          return false;
      }
  }
  </code></pre>
  
  <p>I thought using the <code>using</code> statement is supposed to garbage-collect and release the file after being used. How can I release the file so I can move it and why my file stays locked?</p>
  
  <p><strong>Solved:</strong></p>
  
  <p>Got it. Somewhere between these two calls I was opening a <code>TextReader</code>object without disposing it.</p>