11using Microsoft . Extensions . Configuration ;
22using Microsoft . Web . WebView2 . WinForms ;
3+ using Serilog ;
34using System . Reflection ;
45
56namespace MDriveSync . Client . App . WinForm
@@ -16,38 +17,49 @@ public MainForm()
1617 {
1718 InitializeComponent ( ) ;
1819
19- // 从配置文件中读取 URL,如果没有则使用默认值
20- var configuration = Program . Configuration ;
21- apiUrl = configuration . GetValue < string > ( "urls" ) ? . Replace ( "*" , "localhost" ) ?? "http://localhost:8080" ;
22-
23- // Initialize WebView2
24- webView = new WebView2
20+ try
2521 {
26- Dock = DockStyle . Fill ,
27- Source = new Uri ( $ "{ apiUrl } ") // 指向 Web API 的 URL
28- } ;
29- this . Controls . Add ( webView ) ;
22+ // 从配置文件中读取 URL,如果没有则使用默认值
23+ var configuration = Program . Configuration ;
24+ apiUrl = configuration . GetValue < string > ( "urls" ) ? . Replace ( "*" , "localhost" ) ?? "http://localhost:8080" ;
3025
31- // Initialize NotifyIcon
32- notifyIcon = new NotifyIcon
33- {
34- Icon = this . Icon ,
35- Text = "MDrive" ,
36- Visible = true
37- } ;
26+ // Initialize WebView2
27+ webView = new WebView2
28+ {
29+ Dock = DockStyle . Fill ,
30+ Source = new Uri ( $ "{ apiUrl } ") // 指向 Web API 的 URL
31+ } ;
32+ this . Controls . Add ( webView ) ;
33+
34+ // Initialize NotifyIcon
35+ notifyIcon = new NotifyIcon
36+ {
37+ Icon = this . Icon ,
38+ Text = "MDrive" ,
39+ Visible = true
40+ } ;
3841
39- // 使用资源中的 PNG 图像
40- this . Icon = LoadIconFromResource ( "MDriveSync.Client.WinFormAPI.Resources.logo.png" , 64 , 64 ) ;
42+ // 使用资源中的 PNG 图像
43+ // 获取当前程序集名称,而不是写死
44+ var assemblyName = Assembly . GetExecutingAssembly ( ) . GetName ( ) . Name ;
45+ this . Icon = LoadIconFromResource ( $ "{ assemblyName } .Resources.logo.png", 64 , 64 ) ;
4146
42- notifyIcon . DoubleClick += NotifyIcon_DoubleClick ;
47+ notifyIcon . DoubleClick += NotifyIcon_DoubleClick ;
4348
44- // Initialize ContextMenuStrip
45- contextMenuStrip = new ContextMenuStrip ( ) ;
46- exitMenuItem = new ToolStripMenuItem ( "退出" , null , ExitMenuItem_Click ) ;
47- contextMenuStrip . Items . Add ( exitMenuItem ) ;
48- notifyIcon . ContextMenuStrip = contextMenuStrip ;
49+ // Initialize ContextMenuStrip
50+ contextMenuStrip = new ContextMenuStrip ( ) ;
51+ exitMenuItem = new ToolStripMenuItem ( "退出" , null , ExitMenuItem_Click ) ;
52+ contextMenuStrip . Items . Add ( exitMenuItem ) ;
53+ notifyIcon . ContextMenuStrip = contextMenuStrip ;
4954
50- this . Resize += MainForm_Resize ;
55+ this . Resize += MainForm_Resize ;
56+ }
57+ catch ( Exception ex )
58+ {
59+ Log . Logger . Error ( ex , "初始化失败,请重试" ) ;
60+
61+ MessageBox . Show ( "初始化失败,请重试" ) ;
62+ }
5163 }
5264
5365 private void MainForm_Resize ( object sender , EventArgs e )
0 commit comments