title: C#: Cannot write to a closed TextWriter
body: <p>why when the program hit //HERE. it crashed with    Unhandled Exception: System.ObjectDisposedException: Cannot write to a closed TextWriter.?
  Print() method is called <em>before</em> LogClose(). What I want to achieve is if it hits HERE then print out the message and program exit</p>
  
  <pre><code>foreach (string sf in ff)
  {
  
      if (File.Exists(sf))
      {
          File.Move(sf, copy_dir + "\\" + sf);
      }
      else
      {
          //HERE
          l.ExitAndOut("Error: " + sf + " not found");
      }
  }
  
  
  
  class logger {
  StreamWriter sw = new StreamWriter("SpireCli.log");
  
  public void Print(string log)
  {
      DateTime ts = DateTime.UtcNow;
      sw.WriteLine(ts + " " + log);
  }
  
  public void ExitAndOut(string log)
  {
      string Program_Exit = "Program Exit";
  
      Console.WriteLine(log);
      Console.WriteLine(Program_Exit);
      Print(log);
      Print("Exit");
      LogClose();
      Environment.Exit(1);
  }
  
  public void LogClose()
  {
      sw.Close();
  }
  }
  </code></pre>