日志文件操作(写入,读取,清空)

  #region 日志文件
  ///


  /// 各项校对写入日志文件
  ///

  ///
  public static void WriteCheckInformationToLog(string checkInformation)
  {   
   StreamWriter sw = null;
   StreamWriter swHistory = null;
   try
   {
    sw = new StreamWriter(@".\log.txt",false,Encoding.Default);
    swHistory = new StreamWriter(".\\" + DateTime.Now.ToShortDateString() + "_log.txt",true,Encoding.Default);
    sw.WriteLine(checkInformation);
    swHistory.WriteLine(checkInformation);
   }   
   catch(Exception fileExp)
   {
    throw new Exception("信息写入日志文件失败。系统信息:" + fileExp.Message);
   }
   finally
   {    
    sw.Flush();
    sw.Close();
    swHistory.Flush();
    swHistory.Close();
   }
  }
  ///
  /// 读取日志文件
  ///

  ///
  public void ReadCheckInformationFromLog()
  {
   StreamReader sr = null;
   try
   {
    sr = new StreamReader(".\\log.txt",Encoding.Default,true);   
    sr.ReadLine();
   }   
   catch(Exception fileExp)
   {
    throw new Exception("读取日志文件信息失败。系统信息:" + fileExp.Message);
   }
   finally
   {    
    sr.Close();
   }
  }
    
  ///
  /// 清空日志文件
  ///

  public static void ClearLogFile()
  {
   StreamWriter sw = null;
   
   //清空当前日志文件
   try
   {
    sw = new StreamWriter(@".\log.txt",false,Encoding.Default);   
    sw.WriteLine("");   
    
   }   
   catch(Exception fileExp)
   {
    throw new Exception("清空日志文件失败。系统信息:" + fileExp.Message);
   }
   finally
   {    
    sw.Flush();
    sw.Close();
   }
  }

  #endregion

转载于:https://www.cnblogs.com/super-yc/archive/2006/11/04/549650.html

你可能感兴趣的:(日志文件操作(写入,读取,清空))