我已经看到了这个问题的几个帖子.我已经实现了所有建议,比如在streamwriter和连接对象上使用flush(),close()方法,使用GC.Collect()强制清理,使用{}来自动执行
我正在从DB做简单的操作并写入文本文件..这是我的代码
public void WriteToFile(string ProductName)
{
//Already Got Data from DB and stored in "ProductName"
//saving in File
if (!File.Exists(path11))
{
File.Create(path11);
StreamWriter tw = new StreamWriter(path11);
tw.WriteLine(ProductName+"@"+DateTime.Now.ToString());
tw.Flush();
tw.Close();
}
else if (File.Exists(path11))
{
StreamWriter tw = new StreamWriter(path11, true);
tw.WriteLine(ProductName + "@" + DateTime.Now.ToString());
tw.Flush();
tw.Close();
}
GC.Collect();
}
Run Code Online (Sandbox Code Playgroud)
我得到的另一个建议是锁定对象..但我无法实现它..任何建议都会有帮助