using MaiMuAOI.SysCtrl; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using ToolKits.Log; namespace MaiMuAOI.SysUI.SysSet { public partial class SysLogFrm : Form { private List logPath = new List(); public SysLogFrm() { InitializeComponent(); UIStyle.SetUIStyle(this); this.uiTitel1.FatherForm = this; } //文件按创建日期排序 /// /// 文件按创建日期排序 /// /// /// /// int Compare(Object o1, Object o2) { string fi1 = o1 as string; string fi2 = o2 as string; fi1 = fi1.Replace(".log", "0"); //将非数字字符全转为0 fi1 = fi1.Replace('-', '0'); fi2 = fi2.Replace(".log", "0"); fi2 = fi2.Replace('-', '0'); return Int64.Parse(fi2).CompareTo(Int64.Parse(fi1)); } string[] GetLogFile() { int num = 0; string[] Dirs = Directory.GetDirectories(ConfMgr.Instance.SysConfigParams.LogPath); List list = new List(); for (int i = 0; i < Dirs.Length; i++) { string[] files = Directory.GetFiles(Dirs[i]); list.AddRange(files); } string[] array = list.ToArray(); foreach (string text in array) { logPath.Add(text); array[num] = text.Substring(text.LastIndexOf("\\") + 1); num++; } Array.Sort(array, Compare); return array; } private void SysLogFrm_Load(object sender, EventArgs e) { //加载所有log文件 string[] logfiles = GetLogFile(); this.loglistView.BeginUpdate(); //数据更新,UI暂时挂起,直到EndUpdate绘制控件,可以有效避免闪烁并大大提高加载速度 loglistView.Items.Clear(); for (int i = 0; i < logfiles.Length; i++) { ListViewItem lvi = new ListViewItem(); lvi.Text = logfiles[i]; this.loglistView.Items.Add(lvi); } this.loglistView.EndUpdate(); //结束数据处理,UI界面一次性绘制。 } private void listLogFile_MouseDoubleClick(object sender, MouseEventArgs e) { string path = ""; foreach(var t in logPath) { if (t.IndexOf(loglistView.SelectedItems[0].Text) > 0) { path = t; break; } } if(string.IsNullOrEmpty(path)) return; this.Cursor = Cursors.WaitCursor; //读取文件内容 //string text = System.IO.File.ReadAllText(path); string text = ReadTxtFile(path); tbLog.Text = text; this.Cursor = Cursors.Default; } private string ReadTxtFile(string fileName) { string txt = ""; // var fileDir = this.txtFileFolder.Text.Trim(); var fileDir = fileName; byte[] allBytes = null; byte[] buffer = new byte[1024];//一个1K的缓冲字节容器 Stopwatch stopwatch = new Stopwatch(); stopwatch.Restart(); using (MemoryStream ms = new MemoryStream()) { using (FileStream fs = new FileStream(fileDir, FileMode.Open, FileAccess.Read)) { int positon = 0; while ((positon = fs.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, positon); } allBytes = ms.ToArray(); } } stopwatch.Stop(); // MessageBox.Show($"StreamReader StreamWriter 程序运行花费的时间:{(double)stopwatch.ElapsedMilliseconds / 1000:0.000} secs"); if (null != allBytes) { //尝试将字节转成字符串 txt = System.Text.Encoding.UTF8.GetString(allBytes); // this.richTextBox_Result.Text = txt; } string[] txtToArray = txt.Split('\r'); // ReadData_List.Add(txtToArray); return txt; } } }