我在调试时使用一段代码将一行信息写入文件。
private bool appendLine(string line2Write, string fileName)
{
try
{
StreamWriter tw;
using (tw = File.AppendText(fileName))
{
tw.WriteLine(line2Write);
tw.Close();
}
}
catch (Exception ex)
{
DialogResult result = MessageBox.Show("Unable to write to: " + fileName + "\r\n" + ex.ToString() + "\r\n OK to retry", "File Sysytem Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
if (result == DialogResult.Cancel)
{
return false;
}
}
return true;
}
我不想让文件保持打开状态,因为如果它是调试信息,我不想在程序崩溃时冒最后一点的风险。
我可能不明白 CA2202 告诉我什么。
这是整个错误声明:
警告 CA2202 对象“tw”可以在方法“familyFinances.appendLine(string, string)”中多次处理。为避免生成 System.ObjectDisposedException,您不应在一个对象上多次调用 Dispose。
“tw”只存在于这段代码中。而且,我从来没有以这种方式运行它时出错。
选项或建议?