title: FileStream "cannot access closed file"
body: <p>Why am I receiving the above error message when I use <code>using (fileStream = new FileStream(path, FileMode.Append, FileAccess.Write))</code> but the programme executes perfectly when I replace it with <code>fileStream = File.Create(path);</code> ?</p>
  
  <p>I want to append the console output to an external file. Here's my code:</p>
  
  <pre><code> //copy console output to file in bin\Debug folder
      class ConsoleCopy : IDisposable
      {
          FileStream fileStream;
          StreamWriter fileWriter;
          TextWriter doubleWriter;
          TextWriter oldOut;
          class DoubleWriter : TextWriter
          {
              TextWriter one;
              TextWriter two;
              public DoubleWriter(TextWriter one, TextWriter two)
              {
                  this.one = one;
                  this.two = two;
              }
              public override Encoding Encoding
              {
                  get { return one.Encoding; }
              }
              //Clears all buffers for the current writer
              public override void Flush()
              {
                  one.Flush();
  
                  two.Flush();
              }
              public override void Write(char value)
              {
                  **one.Write(value);**//error thrown here
                  two.Write(value);
              }
          }
          public ConsoleCopy(string path)
          {
              oldOut = Console.Out;
              try
              {
                  **//fileStream = File.Create(path);** //runs alright with this line
                  fileWriter = new StreamWriter(fileStream);
                  fileWriter.AutoFlush = true;
                  doubleWriter = new DoubleWriter(fileWriter, oldOut);
              }
              catch (Exception e)
              {
                  Console.WriteLine("Cannot open file for writing");
                  Console.WriteLine(e.Message);
                  return;
              }
              Console.SetOut(doubleWriter);
          }
          public void Dispose()
          {
              Console.SetOut(oldOut);
              if (fileWriter != null)
              {
                  fileWriter.Flush();
                  fileWriter.Close();
                  fileWriter = null;
              }
              if (fileStream != null)
              {
                  fileStream.Close();
                  fileStream = null;
              }
          }
      }//end of consoleCopy
  </code></pre>
  
  <p>I call this method in my main method as such:</p>
  
  <pre><code> //pass output to ConsoleCopy method for copying to .txt file
          using (var cc = new ConsoleCopy("mylogfile.txt"))
          {
             DateTime theDate = DateTime.UtcNow;
  
              string custom = theDate.ToString("d");
  
              Console.WriteLine("User has logged in on " + custom);
          }
  </code></pre>
  
  <p>UPDATE:</p>
  
  <p>The error was previously shown at <code>one.Write(value)</code>. I've managed to solve it though thanks to Peter. Thanks everyone for your contributions and help :)</p>