当前位置: 代码迷 >> ASP.NET >> 无法写入已关闭的 TextWriter,该怎么解决
  详细解决方案

无法写入已关闭的 TextWriter,该怎么解决

热度:6242   发布时间:2013-02-25 00:00:00.0
无法写入已关闭的 TextWriter
System.ObjectDisposedException: 无法写入已关闭的 TextWriter
连续创建2个文本并且都写入数据。第二个出错。


------解决方案--------------------------------------------------------
{
StreamWriter sr;
sr = File.CreateText(Request.PhysicalApplicationPath + "\\" + "1.txt");
sr.WriteLine("1");
sr.Close();
StreamWriter sw;
sw = File.CreateText(Request.PhysicalApplicationPath + "\\" + "2.txt");
sw .WriteLine("2");
sw .Close(); }


你上面的sr.Close();明显的关闭了,你后面还写当前出错了,改成上面的代码

------解决方案--------------------------------------------------------
要不改成这样也可以,就不用第二个写文件的sw了


StreamWriter sr; 
sr = File.CreateText(Request.PhysicalApplicationPath + "\\" + "1.txt"); 
sr.WriteLine("1"); 
sr.Close();

sr= File.CreateText(Request.PhysicalApplicationPath + "\\" + "2.txt"); 
sr.WriteLine("2"); 
sr.Close(); }
------解决方案--------------------------------------------------------
1.需要你贴下代码
2.估计问题是你还没有搞清楚引用类型的问题
------解决方案--------------------------------------------------------
探讨
C# code {
StreamWriter sr;
sr = File.CreateText(Request.PhysicalApplicationPath + "\\" + "1.txt");
sr.WriteLine("1");
sr.Close();
StreamWriter sw;
sw = File.CreateText(Request.PhysicalApplicationPath + "\\" + "2.txt");
sr.WriteLine("2");
sr.Close();
}


就这个这个。。