-
Notifications
You must be signed in to change notification settings - Fork 0
Rewrite UpdateService as WPF .NET 8 #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <Application x:Class="UpdateService.App" | ||
| xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
| xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
| StartupUri="MainWindow.xaml"> | ||
| <Application.Resources> | ||
| </Application.Resources> | ||
| </Application> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| using System.Windows; | ||
|
|
||
| namespace UpdateService | ||
| { | ||
| public partial class App : System.Windows.Application | ||
| { | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| <Window x:Class="UpdateService.MainWindow" | ||
| xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
| xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
| Title="服務更新工具" | ||
| Height="560" Width="740" | ||
| MinHeight="420" MinWidth="520" | ||
| WindowStartupLocation="CenterScreen" | ||
| ResizeMode="CanResize"> | ||
|
|
||
| <Grid Margin="16"> | ||
| <Grid.RowDefinitions> | ||
| <RowDefinition Height="Auto"/> | ||
| <RowDefinition Height="Auto"/> | ||
| <RowDefinition Height="Auto"/> | ||
| <RowDefinition Height="*"/> | ||
| </Grid.RowDefinitions> | ||
|
|
||
| <!-- 設定區塊 --> | ||
| <GroupBox Grid.Row="0" Header="設定" Margin="0,0,0,12" Padding="10,8"> | ||
| <Grid> | ||
| <Grid.RowDefinitions> | ||
| <RowDefinition Height="Auto"/> | ||
| <RowDefinition Height="10"/> | ||
| <RowDefinition Height="Auto"/> | ||
| </Grid.RowDefinitions> | ||
| <Grid.ColumnDefinitions> | ||
| <ColumnDefinition Width="120"/> | ||
| <ColumnDefinition Width="*"/> | ||
| <ColumnDefinition Width="Auto"/> | ||
| </Grid.ColumnDefinitions> | ||
|
|
||
| <!-- 目的地安裝路徑 --> | ||
| <Label Grid.Row="0" Grid.Column="0" | ||
| Content="目的地安裝路徑:" | ||
| VerticalAlignment="Center"/> | ||
| <TextBox Grid.Row="0" Grid.Column="1" | ||
| x:Name="TxtDestinationFilePath" | ||
| Height="28" VerticalContentAlignment="Center" Padding="4,0"/> | ||
| <Button Grid.Row="0" Grid.Column="2" | ||
| Content="瀏覽..." | ||
| Margin="6,0,0,0" Padding="10,4" Height="28" | ||
| Click="BtnBrowse_Click"/> | ||
|
|
||
| <!-- 安裝模式 --> | ||
| <Label Grid.Row="2" Grid.Column="0" | ||
| Content="安裝模式:" | ||
| VerticalAlignment="Center"/> | ||
| <ComboBox Grid.Row="2" Grid.Column="1" | ||
| x:Name="CmbExecuteMode" | ||
| Height="28" VerticalContentAlignment="Center"> | ||
| <ComboBoxItem Content="完整更新 (Complete)" Tag="Complete"/> | ||
| <ComboBoxItem Content="僅安裝 (Install)" Tag="Install"/> | ||
| <ComboBoxItem Content="僅解除安裝 (UnInstall)" Tag="UnInstall"/> | ||
| </ComboBox> | ||
| </Grid> | ||
| </GroupBox> | ||
|
|
||
| <!-- 執行按鈕列 --> | ||
| <StackPanel Grid.Row="1" Orientation="Horizontal" Margin="0,0,0,8"> | ||
| <Button x:Name="BtnExecute" | ||
| Content="▶ 執行" | ||
| Width="110" Height="32" Padding="8,0" | ||
| Click="BtnExecute_Click"/> | ||
| <Button x:Name="BtnClearLog" | ||
| Content="清除記錄" | ||
| Width="90" Height="32" Padding="8,0" Margin="8,0,0,0" | ||
| Click="BtnClearLog_Click"/> | ||
| </StackPanel> | ||
|
|
||
| <!-- ServiceFiles 路徑提示 --> | ||
| <TextBlock Grid.Row="2" | ||
| x:Name="TxtServiceFilesHint" | ||
| Foreground="Gray" FontSize="11" | ||
| Margin="0,0,0,8" | ||
| TextWrapping="Wrap"/> | ||
|
|
||
| <!-- 執行記錄 --> | ||
| <GroupBox Grid.Row="3" Header="執行記錄"> | ||
| <TextBox x:Name="TxtLog" | ||
| IsReadOnly="True" | ||
| TextWrapping="Wrap" | ||
| VerticalScrollBarVisibility="Auto" | ||
| HorizontalScrollBarVisibility="Auto" | ||
| FontFamily="Consolas" FontSize="12" | ||
| Background="#F8F8F8" | ||
| Padding="6"/> | ||
| </GroupBox> | ||
| </Grid> | ||
| </Window> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| using System.IO; | ||
| using System.Text.Json; | ||
| using System.Windows; | ||
| using System.Windows.Forms; | ||
|
|
||
| namespace UpdateService | ||
| { | ||
| public partial class MainWindow : Window | ||
| { | ||
| private static readonly string AppSettingsPath = | ||
| Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appsettings.json"); | ||
|
|
||
| public MainWindow() | ||
| { | ||
| InitializeComponent(); | ||
| LoadSettings(); | ||
| UpdateServiceFilesHint(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 從 appsettings.json 載入預設設定值 | ||
| /// </summary> | ||
| private void LoadSettings() | ||
| { | ||
| if (File.Exists(AppSettingsPath)) | ||
| { | ||
| try | ||
| { | ||
| var json = File.ReadAllText(AppSettingsPath); | ||
| var doc = JsonDocument.Parse(json); | ||
|
|
||
| if (doc.RootElement.TryGetProperty("DestinationFilePath", out var dest)) | ||
| TxtDestinationFilePath.Text = dest.GetString() ?? string.Empty; | ||
|
|
||
| if (doc.RootElement.TryGetProperty("ExecuteMode", out var mode)) | ||
| { | ||
| var modeStr = mode.GetString() ?? "Complete"; | ||
| foreach (System.Windows.Controls.ComboBoxItem item in CmbExecuteMode.Items) | ||
| { | ||
| if ((string)item.Tag == modeStr) | ||
| { | ||
| CmbExecuteMode.SelectedItem = item; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| catch (JsonException) | ||
| { | ||
| // 設定檔格式有誤,使用預設值 | ||
| } | ||
| catch (IOException) | ||
| { | ||
| // 無法讀取設定檔,使用預設值 | ||
| } | ||
| } | ||
|
|
||
| if (CmbExecuteMode.SelectedItem == null) | ||
| CmbExecuteMode.SelectedIndex = 0; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 更新 ServiceFiles 路徑提示文字 | ||
| /// </summary> | ||
| private void UpdateServiceFilesHint() | ||
| { | ||
| var serviceFilesPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ServiceFiles"); | ||
| TxtServiceFilesHint.Text = $"服務來源資料夾:{serviceFilesPath}"; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 瀏覽目的地資料夾 | ||
| /// </summary> | ||
| private void BtnBrowse_Click(object sender, RoutedEventArgs e) | ||
| { | ||
| using var dialog = new FolderBrowserDialog | ||
| { | ||
| Description = "選擇目的地安裝路徑", | ||
| SelectedPath = TxtDestinationFilePath.Text | ||
| }; | ||
|
|
||
| if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) | ||
| TxtDestinationFilePath.Text = dialog.SelectedPath; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 執行服務更新 | ||
| /// </summary> | ||
| private async void BtnExecute_Click(object sender, RoutedEventArgs e) | ||
| { | ||
| var destinationFilePath = TxtDestinationFilePath.Text.Trim(); | ||
| if (string.IsNullOrEmpty(destinationFilePath)) | ||
| { | ||
| System.Windows.MessageBox.Show("請輸入目的地安裝路徑。", "提示", | ||
| MessageBoxButton.OK, MessageBoxImage.Warning); | ||
| return; | ||
| } | ||
|
|
||
| if (CmbExecuteMode.SelectedItem is not System.Windows.Controls.ComboBoxItem selectedItem) | ||
| return; | ||
|
|
||
| var modeStr = (string)selectedItem.Tag; | ||
| if (!Enum.TryParse<ExecuteModeEnum>(modeStr, out var executeMode)) | ||
| { | ||
| Log("安裝模式設定有誤"); | ||
| return; | ||
| } | ||
|
|
||
| BtnExecute.IsEnabled = false; | ||
| TxtLog.Clear(); | ||
|
|
||
| try | ||
| { | ||
| var service = new UpdateServiceLogic(destinationFilePath, executeMode, Log); | ||
| await service.ExecuteAsync(); | ||
| Log("─── 所有操作已完成 ───"); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Log($"錯誤資訊:{ex.Message}"); | ||
| } | ||
| finally | ||
| { | ||
| BtnExecute.IsEnabled = true; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 清除執行記錄 | ||
| /// </summary> | ||
| private void BtnClearLog_Click(object sender, RoutedEventArgs e) | ||
| { | ||
| TxtLog.Clear(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 將訊息附加至執行記錄(執行緒安全) | ||
| /// </summary> | ||
| private void Log(string message) | ||
| { | ||
| Dispatcher.Invoke(() => | ||
| { | ||
| TxtLog.AppendText($"[{DateTime.Now:HH:mm:ss}] {message}{Environment.NewLine}"); | ||
| TxtLog.ScrollToEnd(); | ||
| }); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.