using System.Configuration; using System.Data; using System.IO; using System.Linq; using System.Windows; namespace DrillTools { /// /// Interaction logic for App.xaml /// 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; } } /// /// 验证是否为有效的钻带文件 /// /// 文件路径 /// 是否为有效钻带文件 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); } } }