144 lines
5.5 KiB
C#
144 lines
5.5 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Data;
|
||
using System.IO;
|
||
using System.Text;
|
||
using System.Windows;
|
||
using System.Windows.Controls;
|
||
using System.Windows.Data;
|
||
using System.Windows.Documents;
|
||
using System.Windows.Input;
|
||
using System.Windows.Media;
|
||
using System.Windows.Media.Imaging;
|
||
using System.Windows.Navigation;
|
||
using System.Windows.Shapes;
|
||
|
||
namespace SunlightAggregationManager.View
|
||
{
|
||
/// <summary>
|
||
/// logPage.xaml 的交互逻辑
|
||
/// </summary>
|
||
public partial class logPage : UserControl
|
||
{
|
||
public logPage()
|
||
{
|
||
InitializeComponent();
|
||
}
|
||
|
||
private readonly string Log_time = DateTime.Now.ToString("yyyy-MM-dd");
|
||
DataTable logdataTable = new DataTable(); //建立缓存
|
||
long TEXT_L;
|
||
|
||
private void logPage_Loaded(object sender, RoutedEventArgs e)//页面打开事件
|
||
{
|
||
comboBox_log.ItemsSource = new string[3] { "Log", "ERR", "Exchange" };
|
||
comboBox_log.Text = "Log";
|
||
|
||
logdataTable.Columns.Add("Name", typeof(string));
|
||
logdataTable.Columns.Add("Length", typeof(int));
|
||
logdataTable.Columns.Add("CreationTimeUtc", typeof(string));
|
||
logdataTable.Columns.Add("LastWriteTimeUtc", typeof(string));
|
||
|
||
DirectoryInfo loginfo = new DirectoryInfo(System.Environment.CurrentDirectory + "\\Log"); //new文件夹
|
||
logdataTable.Clear();
|
||
foreach (var item in loginfo.GetFiles())
|
||
{
|
||
DataRow FileRow = logdataTable.NewRow();
|
||
FileRow["Name"] = item.Name;
|
||
FileRow["Length"] = item.Length / 1024;
|
||
FileRow["CreationTimeUtc"] = item.CreationTimeUtc;
|
||
FileRow["LastWriteTimeUtc"] = item.LastWriteTimeUtc;
|
||
logdataTable.Rows.Add(FileRow);
|
||
}
|
||
logdataTable.DefaultView.Sort = "Name DESC";
|
||
gridLog.ItemsSource = logdataTable.DefaultView.ToTable().DefaultView;
|
||
}
|
||
|
||
private void LOG_file()//建立列表
|
||
{
|
||
DirectoryInfo loginfo = new DirectoryInfo(System.Environment.CurrentDirectory + "\\" + comboBox_log.Text); //new文件夹
|
||
logdataTable.Clear();
|
||
foreach (var item in loginfo.GetFiles())
|
||
{
|
||
DataRow FileRow = logdataTable.NewRow();
|
||
FileRow["Name"] = item.Name;
|
||
FileRow["Length"] = item.Length / 1024;
|
||
FileRow["CreationTimeUtc"] = item.CreationTimeUtc;
|
||
FileRow["LastWriteTimeUtc"] = item.LastWriteTimeUtc;
|
||
logdataTable.Rows.Add(FileRow);
|
||
}
|
||
logdataTable.DefaultView.Sort = "Name DESC";
|
||
gridLog.ItemsSource = logdataTable.DefaultView.ToTable().DefaultView;
|
||
}
|
||
|
||
private void DataGrid_gridLogClick(object sender, MouseButtonEventArgs e)//数据表双击事件
|
||
{
|
||
int rownum = gridLog.SelectedIndex;//获取鼠标选中行并定义变量
|
||
if (rownum != -1)//判断鼠标定位是否有效
|
||
{
|
||
/*定位选中行及指定列单元格文本信息*/
|
||
//获取单元格内容
|
||
var cellContent = gridLog.Columns[0].GetCellContent(gridLog.Items[rownum]);
|
||
//安全转换为 TextBlock 并获取文本
|
||
var textValue = (cellContent as TextBlock)?.Text;
|
||
if (!string.IsNullOrEmpty(textValue))
|
||
{
|
||
LOGDATA_file(textValue);//
|
||
}
|
||
|
||
var Content_L = gridLog.Columns[1].GetCellContent(gridLog.Items[rownum]);
|
||
var Value_L = (Content_L as TextBlock)?.Text.ToString();
|
||
if (!string.IsNullOrEmpty(Value_L))
|
||
{
|
||
TEXT_L = long.Parse(Value_L) * 1024 + 1024;
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 读取文件显示到前端
|
||
/// 调用示例: LOGDATA_file(file path);
|
||
/// </summary>
|
||
/// <param name="path">文件路径</param>
|
||
private async void LOGDATA_file(string dat) //读取文件显示到前端
|
||
{
|
||
Logtext.SelectAll();
|
||
Logtext.Cut();
|
||
Logtext.Document = new FlowDocument();
|
||
string filePath = System.Environment.CurrentDirectory + "\\" + comboBox_log.Text + "\\";
|
||
try
|
||
{
|
||
Logtext.AppendText("Loading...");
|
||
string log_ = "";
|
||
string log_DAT = await Task<string>.Run(() =>
|
||
{
|
||
// 使用StreamReader读取文件
|
||
using (StreamReader reader = new StreamReader(filePath + dat))
|
||
{
|
||
// 读取文件直到文件的末尾
|
||
while (!reader.EndOfStream)
|
||
{
|
||
// 添加文件的每一行到RichTextBox
|
||
log_ = log_ + reader.ReadLine() + "\n";
|
||
}
|
||
}
|
||
return log_;
|
||
});
|
||
Logtext.SelectAll();
|
||
Logtext.Cut();
|
||
Logtext.AppendText(log_DAT);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
// 处理可能发生的任何异常
|
||
MessageBox.Show("Error reading file: " + ex.Message);
|
||
}
|
||
}
|
||
|
||
private void comboBox_log_DropDownClosed(object sender, EventArgs e)
|
||
{
|
||
LOG_file();
|
||
}
|
||
}
|
||
}
|