Files
AohDrllTools/App.xaml.cs
2026-05-16 12:07:32 +08:00

69 lines
2.3 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.Text;
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);
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
// 运行孔位数据功能测试
//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);
}
}
}