版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

145 regels
4.8 KiB

  1. using MaiMuAOI.SysCtrl;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Diagnostics;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. using ToolKits.Log;
  14. namespace MaiMuAOI.SysUI.SysSet
  15. {
  16. public partial class SysLogFrm : Form
  17. {
  18. private List<string> logPath = new List<string>();
  19. public SysLogFrm()
  20. {
  21. InitializeComponent();
  22. UIStyle.SetUIStyle(this);
  23. this.uiTitel1.FatherForm = this;
  24. }
  25. //文件按创建日期排序
  26. /// <summary>
  27. /// 文件按创建日期排序
  28. /// </summary>
  29. /// <param name="o1"></param>
  30. /// <param name="o2"></param>
  31. /// <returns></returns>
  32. int Compare(Object o1, Object o2)
  33. {
  34. string fi1 = o1 as string;
  35. string fi2 = o2 as string;
  36. fi1 = fi1.Replace(".log", "0"); //将非数字字符全转为0
  37. fi1 = fi1.Replace('-', '0');
  38. fi2 = fi2.Replace(".log", "0");
  39. fi2 = fi2.Replace('-', '0');
  40. return Int64.Parse(fi2).CompareTo(Int64.Parse(fi1));
  41. }
  42. string[] GetLogFile()
  43. {
  44. int num = 0;
  45. string[] Dirs = Directory.GetDirectories(ConfMgr.Instance.SysConfigParams.LogPath);
  46. List<string> list = new List<string>();
  47. for (int i = 0; i < Dirs.Length; i++)
  48. {
  49. string[] files = Directory.GetFiles(Dirs[i], "*.log");
  50. list.AddRange(files);
  51. }
  52. string[] array = list.ToArray();
  53. foreach (string text in array)
  54. {
  55. logPath.Add(text);
  56. array[num] = text.Substring(text.LastIndexOf("\\") + 1);
  57. num++;
  58. }
  59. Array.Sort(array, Compare);
  60. return array;
  61. }
  62. private void SysLogFrm_Load(object sender, EventArgs e)
  63. {
  64. //加载所有log文件
  65. string[] logfiles = GetLogFile();
  66. this.loglistView.BeginUpdate(); //数据更新,UI暂时挂起,直到EndUpdate绘制控件,可以有效避免闪烁并大大提高加载速度
  67. loglistView.Items.Clear();
  68. for (int i = 0; i < logfiles.Length; i++)
  69. {
  70. ListViewItem lvi = new ListViewItem();
  71. lvi.Text = logfiles[i];
  72. this.loglistView.Items.Add(lvi);
  73. }
  74. this.loglistView.EndUpdate(); //结束数据处理,UI界面一次性绘制。
  75. }
  76. private void listLogFile_MouseDoubleClick(object sender, MouseEventArgs e)
  77. {
  78. string path = "";
  79. foreach(var t in logPath)
  80. {
  81. if (t.IndexOf(loglistView.SelectedItems[0].Text) > 0)
  82. {
  83. path = t;
  84. break;
  85. }
  86. }
  87. if(string.IsNullOrEmpty(path))
  88. return;
  89. this.Cursor = Cursors.WaitCursor;
  90. //读取文件内容
  91. //string text = System.IO.File.ReadAllText(path);
  92. string text = ReadTxtFile(path);
  93. tbLog.Text = text;
  94. this.Cursor = Cursors.Default;
  95. }
  96. private string ReadTxtFile(string fileName)
  97. {
  98. string txt = "";
  99. // var fileDir = this.txtFileFolder.Text.Trim();
  100. var fileDir = fileName;
  101. byte[] allBytes = null;
  102. byte[] buffer = new byte[1024];//一个1K的缓冲字节容器
  103. Stopwatch stopwatch = new Stopwatch();
  104. stopwatch.Restart();
  105. using (MemoryStream ms = new MemoryStream())
  106. {
  107. using (FileStream fs = new FileStream(fileDir, FileMode.Open, FileAccess.Read))
  108. {
  109. int positon = 0;
  110. while ((positon = fs.Read(buffer, 0, buffer.Length)) > 0)
  111. {
  112. ms.Write(buffer, 0, positon);
  113. }
  114. allBytes = ms.ToArray();
  115. }
  116. }
  117. stopwatch.Stop();
  118. // MessageBox.Show($"StreamReader StreamWriter 程序运行花费的时间:{(double)stopwatch.ElapsedMilliseconds / 1000:0.000} secs");
  119. if (null != allBytes)
  120. {
  121. //尝试将字节转成字符串
  122. txt = System.Text.Encoding.UTF8.GetString(allBytes);
  123. // this.richTextBox_Result.Text = txt;
  124. }
  125. string[] txtToArray = txt.Split('\r');
  126. // ReadData_List.Add(txtToArray);
  127. return txt;
  128. }
  129. }
  130. }