using(StreamWriter w = File.AppendText("test.txt")){ Log("hehe",w); w.close(); } public void static Log(string logMessage,TextWriter w){ w.Write("\r\nLog Entry:"); w.WriteLine(":{0}",logMessage); w.Flush(); }
文件操作或数据库连接等比较消耗资源的操作,使用using在结束时自动释放