123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- using System;
- using System.Text;
- using System.IO;
- using System.Windows.Forms;
- namespace BaseLibRWFile
- {
- public class Log
- {
- public object objLock = new object();
- /// <summary>
- /// 保存log
- /// </summary>
- /// <param name="strFilePath">文件路径</param>
- /// <param name="strFileName">文件名称</param>
- /// <param name="strMsg">写入内容</param>
- /// <returns></returns>
- public bool SaveTxt(string strFilePath, string strFileName, string strMsg)
- {
- lock(objLock)
- {
- try
- {
- strMsg = DateTime.Now.ToString("HH:mm:ss:fff") + "---" + strMsg + "\r\n";
- FileOperate.Directory_Create(strFilePath);
- string strPath = strFilePath + "\\" + strFileName;
- if (!File.Exists(strPath))
- {
- using (FileStream fs = new FileStream(strPath, FileMode.CreateNew))
- fs.Close();
- }
- using (FileStream fsw = new FileStream(strPath, FileMode.Append))
- {
- byte[] mSG_data = Encoding.UTF8.GetBytes(strMsg);
- fsw.Write(mSG_data, 0, mSG_data.Length);
- fsw.Close();
- }
- return true;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString(), "保存text文件出错\r\nSave text error");
- return false;
- }
- }
- }
- }
- }
|