支持拖拽钻带文件到exe自动打开并加载
实现了命令行参数解析,支持将钻带文件(.txt, .drl, .dr2, .dpin)拖拽到exe上直接打开。重构了 App 启动流程,MainWindow 构造函数支持初始文件路径,窗口加载后自动异步读取并解析文件内容。增强了异常处理,提升了启动健壮性和用户体验。
This commit is contained in:
48
App.xaml.cs
48
App.xaml.cs
@@ -1,5 +1,7 @@
|
||||
using System.Configuration;
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
|
||||
namespace DrillTools
|
||||
@@ -16,9 +18,49 @@ namespace DrillTools
|
||||
// 运行孔位数据功能测试
|
||||
//MainWindowViewModel.TestHoleLocationsFunctionality();
|
||||
|
||||
// 创建并显示主窗口
|
||||
//MainWindow mainWindow = new MainWindow();
|
||||
//mainWindow.Show();
|
||||
// 检查是否有命令行参数(拖拽文件到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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user