Files
Better-Seewo/Installer/App.xaml.cs

113 lines
3.6 KiB
C#
Raw Normal View History

/*
* App.xaml.cs - Better-Seewo
*
*
*/
using System;
using System.IO;
using System.Windows;
namespace Better_Seewo.Installer
{
/// <summary>
/// App 的交互逻辑 - 管理安装程序全局状态
/// </summary>
public partial class App : Application
{
/// <summary>
/// 应用程序版本号
/// </summary>
public const string AppVersion = "2.0.0";
/// <summary>
/// 默认安装路径
/// </summary>
public static readonly string DefaultInstallPath =
@"C:\Program Files (x86)\Seewo\EasiNote5\Plugins\Better-EN5";
/// <summary>
/// 当前实际选择的安装路径
/// </summary>
public static string InstallPath { get; set; } = DefaultInstallPath;
/// <summary>
/// 安装是否正在进行中
/// </summary>
public static bool IsInstalling { get; set; }
/// <summary>
/// 安装是否已完成
/// </summary>
public static bool IsInstallCompleted { get; set; }
/// <summary>
/// 安装开始时间(用于计算安装耗时)
/// </summary>
public static DateTime InstallStartTime { get; set; }
/// <summary>
/// 应用程序启动时初始化全局异常处理
/// </summary>
protected override void OnStartup(StartupEventArgs e)
{
// 注册全局未处理异常事件
DispatcherUnhandledException += App_DispatcherUnhandledException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
base.OnStartup(e);
}
/// <summary>
/// UI 线程未处理异常捕获,防止程序崩溃并提示用户
/// </summary>
private void App_DispatcherUnhandledException(object sender,
System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
// 记录异常信息到日志文件
LogError(e.Exception);
// 标记异常已处理,防止程序崩溃
e.Handled = true;
// 弹出友好提示
MessageBox.Show(
$"安装过程中发生错误:\n{e.Exception.Message}\n\n详细信息已记录到日志。",
"Better-Seewo 安装程序",
MessageBoxButton.OK,
MessageBoxImage.Warning
);
}
/// <summary>
/// 非 UI 线程未处理异常捕获
/// </summary>
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
if (e.ExceptionObject is Exception ex)
{
LogError(ex);
}
}
/// <summary>
/// 将错误信息记录到日志文件
/// </summary>
private static void LogError(Exception ex)
{
try
{
// 日志文件存放在临时目录下
string logDir = Path.Combine(Path.GetTempPath(), "Better-Seewo", "Installer", "Logs");
Directory.CreateDirectory(logDir);
string logFile = Path.Combine(logDir, $"error_{DateTime.Now:yyyyMMdd}.log");
string logContent = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {ex}\n\n";
File.AppendAllText(logFile, logContent);
}
catch
{
// 日志写入失败时静默处理
}
}
}
}