/*
* App.xaml.cs - Better-Seewo 安装程序应用程序逻辑
* 负责全局异常处理和安装状态管理
* 作者:雾启工作室
*/
using System;
using System.IO;
using System.Windows;
namespace Better_Seewo.Installer
{
///
/// App 的交互逻辑 - 管理安装程序全局状态
///
public partial class App : Application
{
///
/// 应用程序版本号
///
public const string AppVersion = "2.0.0";
///
/// 默认安装路径
///
public static readonly string DefaultInstallPath =
@"C:\Program Files (x86)\Seewo\EasiNote5\Plugins\Better-EN5";
///
/// 当前实际选择的安装路径
///
public static string InstallPath { get; set; } = DefaultInstallPath;
///
/// 安装是否正在进行中
///
public static bool IsInstalling { get; set; }
///
/// 安装是否已完成
///
public static bool IsInstallCompleted { get; set; }
///
/// 安装开始时间(用于计算安装耗时)
///
public static DateTime InstallStartTime { get; set; }
///
/// 应用程序启动时初始化全局异常处理
///
protected override void OnStartup(StartupEventArgs e)
{
// 注册全局未处理异常事件
DispatcherUnhandledException += App_DispatcherUnhandledException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
base.OnStartup(e);
}
///
/// UI 线程未处理异常捕获,防止程序崩溃并提示用户
///
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
);
}
///
/// 非 UI 线程未处理异常捕获
///
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
if (e.ExceptionObject is Exception ex)
{
LogError(ex);
}
}
///
/// 将错误信息记录到日志文件
///
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
{
// 日志写入失败时静默处理
}
}
}
}