将所有的操作先保存在string strOper中,然后将strOper保存到txt文件中。
其中:
private string strOper=System.DateTime.Now.ToString()+"\r\n";
用来记录当前时间。'\r\n'实现在txt文件中的换行。
![]()
/***记录操作日志***/#region /***记录操作日志***/
private void WriteLog()
![]()
{
try
![]()
{
//判断路径是否存在,不存在创建
string dir=System.IO.Path.GetDirectoryName(logPath);
if(!Directory.Exists(dir))
![]()
{
Directory.CreateDirectory(dir);
}
![]()
// StreamWriter sw = new StreamWriter(logPath,false,System.Text.Encoding.Unicode); //不追加
StreamWriter sw = new StreamWriter(logPath,true); //追加
sw.WriteLine(strOper.ToString());
sw.Close();
}
catch (Exception err)
![]()
{
MessageBox.Show(this,err.Message,"保存操作日志",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
#endregion
![]()
/***查看操作日志***/#region /***查看操作日志***/
private void btnDetails_Click(object sender, System.EventArgs e)
![]()
{
if(File.Exists(this.logPath))
![]()
{
try
![]()
{
System.Diagnostics.Process.Start("notepad.exe",logPath);
![]()
}
catch(Exception err)
![]()
{
MessageBox.Show(this,err.Message,"打开操作日志",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
![]()
}
![]()
#endregion