|
5 | 5 | using Microsoft.Extensions.Logging; |
6 | 6 | using System.IO; |
7 | 7 | using System.Net.Http; |
| 8 | +using System.Runtime.InteropServices; |
8 | 9 | using System.Xml.Serialization; |
9 | 10 | using Windows.ApplicationModel; |
10 | 11 | using Windows.Management.Deployment; |
@@ -123,34 +124,71 @@ public async Task CheckForUpdatesAsync() |
123 | 124 |
|
124 | 125 | public async Task CheckAndUpdateFilesLauncherAsync() |
125 | 126 | { |
126 | | - var destFolderPath = Path.Combine(UserDataPaths.GetDefault().LocalAppData, "Files"); |
127 | | - var destExeFilePath = Path.Combine(destFolderPath, "Files.App.Launcher.exe"); |
128 | | - |
129 | | - if (File.Exists(destExeFilePath)) |
| 127 | + try |
130 | 128 | { |
| 129 | + var destFolderPath = Path.Combine(UserDataPaths.GetDefault().LocalAppData, "Files"); |
| 130 | + var destExeFilePath = Path.Combine(destFolderPath, "Files.App.Launcher.exe"); |
| 131 | + |
| 132 | + if (!File.Exists(destExeFilePath)) |
| 133 | + return; |
| 134 | + |
131 | 135 | var hashEqual = false; |
132 | | - var srcHashFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/FilesOpenDialog/Files.App.Launcher.exe.sha256")); |
| 136 | + var srcHashFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/FilesOpenDialog/Files.App.Launcher.exe.sha256")) |
| 137 | + .AsTask().ConfigureAwait(false); |
133 | 138 | var destHashFilePath = Path.Combine(destFolderPath, "Files.App.Launcher.exe.sha256"); |
134 | 139 |
|
135 | 140 | if (File.Exists(destHashFilePath)) |
136 | 141 | { |
137 | | - await using var srcStream = (await srcHashFile.OpenReadAsync()).AsStream(); |
138 | | - await using var destStream = File.OpenRead(destHashFilePath); |
139 | | - |
140 | | - hashEqual = HashEqual(srcStream, destStream); |
| 142 | + try |
| 143 | + { |
| 144 | + await using var srcStream = (await srcHashFile.OpenReadAsync().AsTask().ConfigureAwait(false)).AsStream(); |
| 145 | + await using var destStream = File.OpenRead(destHashFilePath); |
| 146 | + hashEqual = HashEqual(srcStream, destStream); |
| 147 | + } |
| 148 | + catch (COMException ex) |
| 149 | + { |
| 150 | + Logger?.LogWarning(ex, "Failed to compare hash files"); |
| 151 | + return; |
| 152 | + } |
| 153 | + catch (IOException ex) |
| 154 | + { |
| 155 | + Logger?.LogWarning(ex, "IO error while reading hash files"); |
| 156 | + return; |
| 157 | + } |
141 | 158 | } |
142 | 159 |
|
143 | 160 | if (!hashEqual) |
144 | 161 | { |
145 | | - var srcExeFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/FilesOpenDialog/Files.App.Launcher.exe")); |
146 | | - var destFolder = await StorageFolder.GetFolderFromPathAsync(destFolderPath); |
147 | | - |
148 | | - await srcExeFile.CopyAsync(destFolder, "Files.App.Launcher.exe", NameCollisionOption.ReplaceExisting); |
149 | | - await srcHashFile.CopyAsync(destFolder, "Files.App.Launcher.exe.sha256", NameCollisionOption.ReplaceExisting); |
150 | | - |
151 | | - Logger?.LogInformation("Files.App.Launcher updated."); |
| 162 | + try |
| 163 | + { |
| 164 | + var srcExeFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/FilesOpenDialog/Files.App.Launcher.exe")) |
| 165 | + .AsTask().ConfigureAwait(false); |
| 166 | + var destFolder = await StorageFolder.GetFolderFromPathAsync(destFolderPath).AsTask().ConfigureAwait(false); |
| 167 | + |
| 168 | + await srcExeFile.CopyAsync(destFolder, "Files.App.Launcher.exe", NameCollisionOption.ReplaceExisting) |
| 169 | + .AsTask().ConfigureAwait(false); |
| 170 | + await srcHashFile.CopyAsync(destFolder, "Files.App.Launcher.exe.sha256", NameCollisionOption.ReplaceExisting) |
| 171 | + .AsTask().ConfigureAwait(false); |
| 172 | + |
| 173 | + Logger?.LogInformation("Files.App.Launcher updated."); |
| 174 | + } |
| 175 | + catch (COMException ex) |
| 176 | + { |
| 177 | + Logger?.LogError(ex, ex.Message); |
| 178 | + return; |
| 179 | + } |
| 180 | + catch (UnauthorizedAccessException ex) |
| 181 | + { |
| 182 | + Logger?.LogError(ex, ex.Message); |
| 183 | + return; |
| 184 | + } |
152 | 185 | } |
153 | 186 | } |
| 187 | + catch (Exception ex) |
| 188 | + { |
| 189 | + Logger?.LogError(ex, ex.Message); |
| 190 | + return; |
| 191 | + } |
154 | 192 |
|
155 | 193 | bool HashEqual(Stream a, Stream b) |
156 | 194 | { |
|
0 commit comments