将所有的操作先保存在string strOper中,然后将strOper保存到txt文件中。
其中:
private string strOper=System.DateTime.Now.ToString()+"\r\n";
用来记录当前时间。'\r\n'实现在txt文件中的换行。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
/***记录操作日志***/#region /***记录操作日志***/
private void WriteLog()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//判断路径是否存在,不存在创建
string dir=System.IO.Path.GetDirectoryName(logPath);
if(!Directory.Exists(dir))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Directory.CreateDirectory(dir);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 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)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show(this,err.Message,"保存操作日志",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
/***查看操作日志***/#region /***查看操作日志***/
private void btnDetails_Click(object sender, System.EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(File.Exists(this.logPath))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Diagnostics.Process.Start("notepad.exe",logPath);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
catch(Exception err)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show(this,err.Message,"打开操作日志",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion