关键词搜索

源码搜索 ×
×

C# 将日志写入文本文件

发布2014-01-08浏览6997次

详情内容

  1. <span style="font-size:14px;"> <span style="white-space:pre"> </span>/// <summary>
  2. /// 记录文本文件日志方法
  3. /// </summary>
  4. /// <param name="FileContent">需要记录的文件内容</param>
  5. /// <param name="TxtFileName">保存的文件名</param>
  6. /// <param name="ErrMsg">错误信息</param>
  7. /// <returns></returns>
  8. public static bool WriteFile(string FileContent, string TxtFileName, out string ErrMsg)
  9. {
  10. ErrMsg = string.Empty;
  11. StreamWriter writer = null;
  12. string sCurDate = System.DateTime.Now.ToString("yyyy-MM-dd");
  13. string sFile = sCurDate + TxtFileName + ".txt";
  14. sFile = "D:\\Log\\Log001.txt";
  15. try
  16. {
  17. if (File.Exists(sFile))
  18. writer = new StreamWriter(sFile, true, System.Text.Encoding.GetEncoding("UTF-8"));
  19. else
  20. writer = new StreamWriter(sFile, false, System.Text.Encoding.GetEncoding("UTF-8"));
  21. string sDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:sss");
  22. writer.WriteLine("<" + sDateTime + "> " + " " + FileContent);
  23. }
  24. catch (IOException e)
  25. {
  26. ErrMsg = e.Message;
  27. return false;
  28. }
  29. finally
  30. {
  31. if (writer != null)
  32. writer.Close();
  33. }
  34. return true;
  35. }</span>

小注:
         路径文件夹需要服务everyone完全的权限。
版本二(可以创建文件夹目录)
  1. <span style="font-size:14px;"> /// <summary>
  2. /// 记录文本文件日志方法
  3. /// </summary>
  4. /// <param name="FileContent">需要记录的文件内容</param>
  5. /// <param name="TxtFileName">保存的文件名</param>
  6. /// <param name="SubFolder">子文件名</param>
  7. /// <param name="ErrMsg">错误信息</param>
  8. /// <returns></returns>
  9. public static bool WriteFile(string FileContent, string TxtFileName, string SubFolder, out string ErrMsg)
  10. {
  11. ErrMsg = string.Empty;
  12. StreamWriter writer = null;
  13. string sCurDate = System.DateTime.Now.ToString("yyyy-MM-dd");
  14. string sFile = sCurDate + TxtFileName + ".txt";
  15. string sFolder = string.Empty;
  16. if (SubFolder != null && SubFolder.Trim().Length > 0)
  17. {
  18. sFile = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Log\\" + SubFolder + "\\" + sFile;
  19. sFolder = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Log\\" + SubFolder;
  20. }
  21. else
  22. {
  23. sFile = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Log\\" + sFile;
  24. sFolder = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Log\\";
  25. }
  26. try
  27. {
  28. if (!Directory.Exists(sFolder))
  29. {
  30. Directory.CreateDirectory(sFolder);
  31. }
  32. if (File.Exists(sFile))
  33. {
  34. writer = new StreamWriter(sFile, true, System.Text.Encoding.GetEncoding("UTF-8"));
  35. }
  36. else
  37. {
  38. writer = new StreamWriter(sFile, false, System.Text.Encoding.GetEncoding("UTF-8"));
  39. }
  40. string sDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:sss");
  41. writer.WriteLine("<" + sDateTime + "> " + " " + FileContent);
  42. }
  43. catch (IOException e)
  44. {
  45. ErrMsg = e.ToString();
  46. return false;
  47. }
  48. finally
  49. {
  50. if (writer != null)
  51. {
  52. writer.Close();
  53. }
  54. }
  55. return true;
  56. }</span>



相关技术文章

最新源码

下载排行榜

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载