C# try 和 using()括号

1、一定要把可能会出错的代码段放入try,否则程序有崩溃的危险。

2、自己懒得手动释放资源或者来不及释放资源的时候就用using() 吧,读写文件的时候会很有用。

 

using (StreamWriter sw = new StreamWriter(LOG_FILE, true, System.Text.Encoding.UTF8))
{
  sw.Write(log);
  sw.Flush();
  sw.Close();
  sw.Dispose();
}

 

posted @ 2009-07-16 17:15  afish1984  阅读(439)  评论(0编辑  收藏  举报