Log.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using System.Text;
  3. using System.IO;
  4. using System.Windows.Forms;
  5. namespace BaseLibRWFile
  6. {
  7. public class Log
  8. {
  9. public object objLock = new object();
  10. /// <summary>
  11. /// 保存log
  12. /// </summary>
  13. /// <param name="strFilePath">文件路径</param>
  14. /// <param name="strFileName">文件名称</param>
  15. /// <param name="strMsg">写入内容</param>
  16. /// <returns></returns>
  17. public bool SaveTxt(string strFilePath, string strFileName, string strMsg)
  18. {
  19. lock(objLock)
  20. {
  21. try
  22. {
  23. strMsg = DateTime.Now.ToString("HH:mm:ss:fff") + "---" + strMsg + "\r\n";
  24. FileOperate.Directory_Create(strFilePath);
  25. string strPath = strFilePath + "\\" + strFileName;
  26. if (!File.Exists(strPath))
  27. {
  28. using (FileStream fs = new FileStream(strPath, FileMode.CreateNew))
  29. fs.Close();
  30. }
  31. using (FileStream fsw = new FileStream(strPath, FileMode.Append))
  32. {
  33. byte[] mSG_data = Encoding.UTF8.GetBytes(strMsg);
  34. fsw.Write(mSG_data, 0, mSG_data.Length);
  35. fsw.Close();
  36. }
  37. return true;
  38. }
  39. catch (Exception ex)
  40. {
  41. MessageBox.Show(ex.ToString(), "保存text文件出错\r\nSave text error");
  42. return false;
  43. }
  44. }
  45. }
  46. }
  47. }