Files
FT479/WPFWCSDemo/ViewModels/LogViewModel.cs
2026-06-29 14:46:32 +08:00

202 lines
6.4 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows;
using CommunityToolkit.Mvvm.ComponentModel;
using System.Reactive.Linq;
using System.Diagnostics;
using System.Threading;
namespace WPFWCSDemo.ViewModels
{
public partial class LogViewModel : ObservableObject
{
public LogViewModel()
{
//更新(当日)日志
//Task.Run(() =>
//{
// while (true)
// {
// try
// {
// //获取存储日志的字符串
// Content = File.ReadAllText(dirPath + @"logs\2024-10-09\Debug.txt");
// Task.Delay(1000);
// }
// catch (Exception)
// {
// }
// }
//});
}
//存放日志文件路径
//readonly string dirPath = @"E:\VSProject\WPF\FT472\WPFWCSDemo\bin\Debug\net6.0-windows\";
//[ObservableProperty]
//string _content;
/*
#region 下拉框内容初始化
//月份选择下拉框
cbxMonth.Items.Clear();
cbxMonth.Items.Add("-请选择月份-");
cbxMonth.Items.Add("一月");
cbxMonth.Items.Add("二月");
cbxMonth.Items.Add("三月");
cbxMonth.Items.Add("四月");
cbxMonth.Items.Add("五月");
cbxMonth.Items.Add("六月");
cbxMonth.Items.Add("七月");
cbxMonth.Items.Add("八月");
cbxMonth.Items.Add("九月");
cbxMonth.Items.Add("十月");
cbxMonth.Items.Add("十一月");
cbxMonth.Items.Add("十二月");
//下拉框选择初始化
cbxMonth.SelectedIndex = 0;
cbxLogName.SelectedIndex = 0;
#endregion
}
/// <summary>
/// 实时日志【内容更新】事件触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtLog_TextChanged(object sender, EventArgs e)
{
//【搜索】按钮打开当天日志时,令滚动条位于最下
txtLog.SelectionStart = txtLog.Text.Length;
txtLog.ScrollToCaret();
}
/// <summary>
/// 【月份】下拉框的值改变事件--→【日志】名称选择的下拉框也随之变化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cbxMonth_TextChanged(object sender, EventArgs e)
{
//日志文件名选择下拉框
cbxLogName.Items.Clear();
//获取存放日志文件夹所有文件全路径
try
{
//尝试获取路径,否则报错
string[] name = Directory.GetDirectories(dirPath);
//添加到下拉框
cbxLogName.Items.Add("-请选择日志-");
for (int i = 0; i < name.Length; i++)
{
//从文件名筛选符合月份条件的项
if (Path.GetFileNameWithoutExtension(name[i]).Contains("-" + cbxMonth.SelectedIndex.ToString("00") + "-"))
{
//添加当月日志的文件名
cbxLogName.Items.Add(Path.GetFileNameWithoutExtension(name[i]));
}
}
cbxLogName.SelectedIndex = 0;
}
catch (Exception)
{
return;
}
}
/// <summary>
/// 【搜索】按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSearch_Click(object sender, EventArgs e)
{
if (cbxMonth.SelectedIndex != 0 && cbxLogName.SelectedIndex == 0)
{
MessageBox.Show("请选择要查找历史日志的时间!");
return;
}
if (cbxMonth.SelectedIndex == 0 && cbxLogName.SelectedIndex == 0) return;
try
{
//判断选择的日志文件日期是否为当天
if (cbxLogName.Text.Equals(DateTime.Now.ToString("yyyy-MM-dd")))
{
//显示实时日志
txtLog.Visible = true;
}
else
{
if (cbxMonth.SelectedIndex == 0 && cbxLogName.SelectedIndex == 0) return;
//获取选中的路径文件内容
txtLogHistory.Text = File.ReadAllText(dirPath + cbxLogName.Text + @"/Info.log", Encoding.Default);
//隐藏实时日志---显示历史日志
txtLog.Visible = false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
/// <summary>
/// 【取消/刷新】页面按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCancel_Click(object sender, EventArgs e)
{
//【搜索】按钮打开当天日志时,令滚动条位于最下,相比根据【内容更新】事件触发此效果可能存在的时间间隔有观感提升
txtLog.SelectionStart = txtLog.Text.Length;
txtLog.ScrollToCaret();
//显示实时日志
txtLog.Visible = true;
}
/// <summary>
/// 打开当日的日志文档
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnOpenFile_Click(object sender, EventArgs e)
{
//打开当日的日志文档
//Process.Start("notepad", dirPath + DateTime.Now.ToString("yyyy-MM-dd") + "/Info.log");
if (Directory.Exists(dirPath + DateTime.Now.ToString("yyyy-MM-dd")))
{
Process.Start(dirPath + DateTime.Now.ToString("yyyy-MM-dd"));
}
else
{
Process.Start(dirPath);
}
}
*/
}
}