標籤:winform http c com string 檔案
轉載地址:夏日裡的春天的部落格 http://hi.baidu.com/honfei/item/3a6f212998910099b73263b5
//出錯之後計入記錄檔
private void SqlConnError(SqlException e2)
{
//如果是同一天的話,則開啟檔案在末尾寫入。如果不是同一天,則建立檔案寫入檔案
//判斷是否存在檔案
if (File.Exists(DateTime.Today.ToString("yyyyMMdd") + ".log"))
{
//如果存在檔案,則向檔案添加日誌
StreamWriter sw =new StreamWriter(DateTime.Today.ToString("yyyyMMdd") + ".log",true);
sw.WriteLine("============================================================================");
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")+":");
sw.WriteLine(e2.Message);
sw.Close();
return;
}
//如果檔案不存在,則建立檔案後向檔案添加日誌
StreamWriter sw2 = new StreamWriter(DateTime.Today.ToString("yyyyMMdd") + ".log",true);
sw2.WriteLine("============================================================================");
sw2.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":");
sw2.WriteLine(e2.Message);
sw2.Close();
}
//記錄檔結束
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
上面的方法的參數是sqlException類型的異常,你也可以重載此方法,用來處理所有異常,如果你改動參數類型的話(比如改成string或其他物件類型,下面的代碼也要做相應的轉換<最終要轉換成string類型>)
注意要對檔案進行操作要引入System.IO;
using System.IO;