Files
AohDrllTools/App.xaml.cs
Mr.Xia 6361f17598 支持拖拽钻带文件到exe自动打开并加载
实现了命令行参数解析,支持将钻带文件(.txt, .drl, .dr2, .dpin)拖拽到exe上直接打开。重构了 App 启动流程,MainWindow 构造函数支持初始文件路径,窗口加载后自动异步读取并解析文件内容。增强了异常处理,提升了启动健壮性和用户体验。
2025-12-30 18:42:03 +08:00

66 lines
2.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Windows;
namespace DrillTools
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : System.Windows.Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// 运行孔位数据功能测试
//MainWindowViewModel.TestHoleLocationsFunctionality();
// 检查是否有命令行参数拖拽文件到exe上
string? filePath = null;
if (e.Args.Length > 0)
{
// 获取第一个参数作为文件路径
filePath = e.Args[0];
// 验证文件是否存在且为有效的钻带文件
if (!IsValidDrillTapeFile(filePath))
filePath = null; // 重置为null按正常启动流程
}
try
{
MainWindow mainWindow = new MainWindow(filePath);
mainWindow.Show();
}
catch (Exception ex)
{
// 显示错误消息
System.Windows.MessageBox.Show($"创建MainWindow时发生异常:\n{ex.GetType().Name} - {ex.Message}\n\n{ex.StackTrace}",
"启动失败", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
throw;
}
}
/// <summary>
/// 验证是否为有效的钻带文件
/// </summary>
/// <param name="filePath">文件路径</param>
/// <returns>是否为有效钻带文件</returns>
private static bool IsValidDrillTapeFile(string filePath)
{
if (string.IsNullOrEmpty(filePath))
return false;
if (!File.Exists(filePath))
return false;
// 检查文件扩展名是否为支持的钻带文件格式
string extension = Path.GetExtension(filePath).ToLowerInvariant();
string[] supportedExtensions = { ".txt", ".drl", ".dr2", ".dpin" };
return supportedExtensions.Contains(extension);
}
}
}