-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
149 lines (132 loc) · 5.22 KB
/
Copy pathProgram.cs
File metadata and controls
149 lines (132 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml;
namespace FastTransfer;
public static class Program
{
private const string RuntimeUrl = "https://aka.ms/windowsappsdk/1.8/runtime/win-x64";
private const string DotNetUrl = "https://dotnet.microsoft.com/download/dotnet/8.0";
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern int MessageBox(IntPtr hWnd, string text, string caption, uint type);
private const uint MB_OK = 0x00000000;
private const uint MB_ICONERROR = 0x00000010;
private const uint MB_ICONWARNING = 0x00000030;
private const uint MB_ICONINFORMATION = 0x00000040;
[STAThread]
private static void Main(string[] args)
{
// 运行时检测:检查 Windows App SDK 运行时是否可用
if (!CheckWindowsAppSdkRuntime())
{
ShowMissingRuntimeDialog("Windows App SDK 运行时",
"FastTransfer 需要 Windows App SDK 1.8 运行时才能运行。\n\n" +
"请点击「确定」打开下载页面,安装后重新启动程序。",
RuntimeUrl);
return;
}
// 检查 64 位进程
if (!Environment.Is64BitProcess)
{
ShowMissingRuntimeDialog("系统架构不匹配",
"FastTransfer 需要 64 位 Windows 系统。",
DotNetUrl);
return;
}
try
{
// PublishSingleFile 需要设置此环境变量以便 Windows App SDK 运行时找到资源
Environment.SetEnvironmentVariable("MICROSOFT_WINDOWSAPPRUNTIME_BASE_DIRECTORY", AppContext.BaseDirectory);
WinRT.ComWrappersSupport.InitializeComWrappers();
Application.Start(_ =>
{
var context = new DispatcherQueueSynchronizationContext(DispatcherQueue.GetForCurrentThread());
SynchronizationContext.SetSynchronizationContext(context);
new App();
});
}
catch (Exception ex)
{
// 应用启动失败,显示错误信息并打开下载页面
MessageBox(IntPtr.Zero,
$"FastTransfer 启动失败:{ex.Message}\n\n" +
$"请确保已安装最新版 Windows App SDK 运行时。\n\n" +
"点击「确定」打开下载页面。",
"FastTransfer - 启动错误",
MB_OK | MB_ICONERROR);
try { Process.Start(new ProcessStartInfo(RuntimeUrl) { UseShellExecute = true }); }
catch { }
}
}
/// <summary>检查 Windows App SDK 运行时是否可用。</summary>
private static bool CheckWindowsAppSdkRuntime()
{
try
{
// 尝试获取 WinUI 类型,如果能成功说明运行时已加载
try
{
var assembly = typeof(Microsoft.UI.Xaml.Application).Assembly;
return assembly != null;
}
catch
{
// WinUI 类型无法加载,继续检查其他方式
}
// 检查注册表
using var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(
@"SOFTWARE\Microsoft\WindowsAppSDK\1.8");
if (key != null) return true;
// 检查环境变量
var wsdk = Environment.GetEnvironmentVariable("WINDOWSAPPSDK_RUNTIME_PATH");
if (!string.IsNullOrWhiteSpace(wsdk) && Directory.Exists(wsdk))
return true;
// 检查常见安装路径
var programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
var runtimeDir = Path.Combine(programFiles, "WindowsApps",
"Microsoft.WindowsAppRuntime.1.8");
if (Directory.Exists(runtimeDir))
return true;
// 检查输出目录
var baseDir = AppContext.BaseDirectory;
if (Directory.Exists(baseDir) &&
Directory.GetFiles(baseDir, "Microsoft.WindowsAppRuntime*.dll").Length > 0)
return true;
return false;
}
catch
{
// 检测出错时默认允许尝试运行
return true;
}
}
/// <summary>显示运行时缺失对话框,并提供下载链接。</summary>
private static void ShowMissingRuntimeDialog(string title, string message, string downloadUrl)
{
var result = MessageBox(IntPtr.Zero,
message + "\n\n点击「确定」打开下载页面。",
$"FastTransfer - 缺少 {title}",
MB_OK | MB_ICONWARNING);
if (result == 1) // IDOK
{
try
{
Process.Start(new ProcessStartInfo(downloadUrl)
{
UseShellExecute = true,
Verb = "open"
});
}
catch
{
MessageBox(IntPtr.Zero,
$"无法打开浏览器。请手动访问:\n{downloadUrl}",
"FastTransfer",
MB_OK | MB_ICONINFORMATION);
}
}
}
}