为什么当程序点击//这里。它因未处理的异常而崩溃:System.ObjectDisposedException:无法写入已关闭的 TextWriter。?Print() 方法在LogClose()之前调用。我想要实现的是如果它点击这里然后打印出消息和程序退出
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();
}
}