终端一体化运控平台
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TextHelper.cs 2.7 KiB

2 년 전
2 년 전
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace BPASmartClient.Helper
  8. {
  9. public class TextHelper
  10. {
  11. private volatile static TextHelper _Instance;
  12. public static TextHelper GetInstance => _Instance ?? (_Instance = new TextHelper());
  13. private TextHelper() { }
  14. /// <summary>
  15. /// 保存日志信息
  16. /// </summary>
  17. /// <param name="info"></param>
  18. public void SaveLogInfo(string info, string name, string DicrectoryName = "Log")
  19. {
  20. if (info?.Length > 0)
  21. {
  22. Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{LocaPath.GetInstance().FilePath}\\{DicrectoryName}"));
  23. string path = $"{AppDomain.CurrentDomain.BaseDirectory}{LocaPath.GetInstance().FilePath}\\{DicrectoryName}\\{DateTime.Now.ToString("yyyy-MM-dd") + " " + name}.txt";
  24. StringBuilder sb = new StringBuilder();
  25. sb.Append($"****************************************** {DateTime.Now} ******************************************" + "\n");
  26. sb.Append(info);
  27. sb.Append("**********************************************************************************************************" + "\n\n");
  28. FileStream fs = new FileStream($"{path}", FileMode.Append);
  29. StreamWriter sw = new StreamWriter(fs);
  30. sw.WriteLine(sb.ToString());
  31. sw.Close();
  32. fs.Close();
  33. }
  34. }
  35. public string ReadTextInfo(string fileName, string DicrectoryName = "")
  36. {
  37. Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"AccessFile\\{DicrectoryName}"));
  38. string path = $"{AppDomain.CurrentDomain.BaseDirectory}AccessFile\\{DicrectoryName}\\{fileName}.txt";
  39. FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
  40. StreamReader sr = new StreamReader(fs);
  41. string GetStr = sr.ReadLine();
  42. if (GetStr == null) GetStr = string.Empty;
  43. sr.Close();
  44. fs.Close();
  45. return GetStr;
  46. }
  47. public void WriteTextInfo(string info, string fileName, string DicrectoryName = "")
  48. {
  49. Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"AccessFile\\{DicrectoryName}"));
  50. string path = $"{AppDomain.CurrentDomain.BaseDirectory}AccessFile\\{DicrectoryName}\\{fileName}.txt";
  51. FileStream fs = new FileStream(path, FileMode.Create);
  52. StreamWriter sw = new StreamWriter(fs);
  53. sw.WriteLine(info);
  54. sw.Close();
  55. fs.Close();
  56. }
  57. }
  58. }