写日志
public void writelog(string strlog)
{
string sfilepath = appdomain.currentdomain.basedirectory + "log";
string sfilename = datetime.now.tostring("yyyymmdd") + ".txt";
sfilename = sfilepath + "\\" + sfilename; //文件的绝对路径
if (!directory.exists(sfilepath))//验证路径是否存在
{
directory.createdirectory(sfilepath);
//不存在则创建
}
filestream fs;
streamwriter sw;
if (file.exists(sfilename))
//验证文件是否存在,有则追加,无则创建
{
fs = new filestream(sfilename, filemode.append, fileaccess.write);
}
else
{
fs = new filestream(sfilename, filemode.create, fileaccess.write);
}
sw = new streamwriter(fs);
sw.writeline(datetime.now.tostring("yyyy-mm-dd hh:mm:ss") + " --- " + strlog);
sw.close();
fs.close();
}
上一篇: 夜光眼