|
| 1 | +# Electron.AutoUpdater |
| 2 | + |
| 3 | +Handle application updates and installation processes. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The `Electron.AutoUpdater` API provides comprehensive functionality for handling application updates, including checking for updates, downloading, and installation. It supports multiple update providers and platforms with automatic update capabilities. |
| 8 | + |
| 9 | +## Key Methods |
| 10 | + |
| 11 | +### Update Checking |
| 12 | + |
| 13 | +#### CheckForUpdatesAsync() |
| 14 | + |
| 15 | +**Returns:** UpdateCheckResult with information about available updates. |
| 16 | + |
| 17 | +**Description:** Asks the server whether there is an update. |
| 18 | + |
| 19 | +#### CheckForUpdatesAndNotifyAsync() |
| 20 | + |
| 21 | +**Returns:** UpdateCheckResult with information about available updates. |
| 22 | + |
| 23 | +**Description:** Asks the server whether there is an update and notifies the user if an update is available. |
| 24 | + |
| 25 | +### Update Management |
| 26 | + |
| 27 | +#### DownloadUpdateAsync() |
| 28 | + |
| 29 | +**Returns:** Path to downloaded file. |
| 30 | + |
| 31 | +**Description:** Start downloading update manually. Use this method if AutoDownload option is set to false. |
| 32 | + |
| 33 | +#### QuitAndInstall(bool, bool) |
| 34 | + |
| 35 | +**Parameters:** |
| 36 | +- `isSilent` (optional) - Windows-only: Runs the installer in silent mode. Defaults to false |
| 37 | +- `isForceRunAfter` - Run the app after finish even on silent install. Not applicable for macOS |
| 38 | + |
| 39 | +**Description:** Restarts the app and installs the update after it has been downloaded. Should only be called after update-downloaded has been emitted. |
| 40 | + |
| 41 | +### Configuration |
| 42 | + |
| 43 | +#### GetFeedURLAsync() |
| 44 | + |
| 45 | +**Returns:** Feed URL. |
| 46 | + |
| 47 | +**Description:** Get the current feed URL. |
| 48 | + |
| 49 | +## Properties |
| 50 | + |
| 51 | +### AutoDownload |
| 52 | + |
| 53 | +**Type:** bool |
| 54 | + |
| 55 | +**Description:** Whether to automatically download an update when it is found. Default is true. |
| 56 | + |
| 57 | +### AutoInstallOnAppQuit |
| 58 | + |
| 59 | +**Type:** bool |
| 60 | + |
| 61 | +**Description:** Whether to automatically install a downloaded update on app quit. Applicable only on Windows and Linux. |
| 62 | + |
| 63 | +### AllowPrerelease |
| 64 | + |
| 65 | +**Type:** bool |
| 66 | + |
| 67 | +**Description:** GitHub provider only. Whether to allow update to pre-release versions. Defaults to true if application version contains prerelease components. |
| 68 | + |
| 69 | +### FullChangelog |
| 70 | + |
| 71 | +**Type:** bool |
| 72 | + |
| 73 | +**Description:** GitHub provider only. Get all release notes (from current version to latest), not just the latest. Default is false. |
| 74 | + |
| 75 | +### AllowDowngrade |
| 76 | + |
| 77 | +**Type:** bool |
| 78 | + |
| 79 | +**Description:** Whether to allow version downgrade. Default is false. |
| 80 | + |
| 81 | +### UpdateConfigPath |
| 82 | + |
| 83 | +**Type:** string |
| 84 | + |
| 85 | +**Description:** For test only. Configuration path for updates. |
| 86 | + |
| 87 | +### CurrentVersionAsync |
| 88 | + |
| 89 | +**Type:** Task<SemVer> |
| 90 | + |
| 91 | +**Returns:** The current application version. |
| 92 | + |
| 93 | +**Description:** Get the current application version. |
| 94 | + |
| 95 | +### ChannelAsync |
| 96 | + |
| 97 | +**Type:** Task<string> |
| 98 | + |
| 99 | +**Returns:** The update channel. |
| 100 | + |
| 101 | +**Description:** Get the update channel. Not applicable for GitHub. |
| 102 | + |
| 103 | +### RequestHeadersAsync |
| 104 | + |
| 105 | +**Type:** Task<Dictionary<string, string>> |
| 106 | + |
| 107 | +**Returns:** The request headers. |
| 108 | + |
| 109 | +**Description:** Get the current request headers. |
| 110 | + |
| 111 | +## Events |
| 112 | + |
| 113 | +### OnError |
| 114 | + |
| 115 | +**Description:** Emitted when there is an error while updating. |
| 116 | + |
| 117 | +### OnCheckingForUpdate |
| 118 | + |
| 119 | +**Description:** Emitted when checking if an update has started. |
| 120 | + |
| 121 | +### OnUpdateAvailable |
| 122 | + |
| 123 | +**Description:** Emitted when there is an available update. The update is downloaded automatically if AutoDownload is true. |
| 124 | + |
| 125 | +### OnUpdateNotAvailable |
| 126 | + |
| 127 | +**Description:** Emitted when there is no available update. |
| 128 | + |
| 129 | +### OnDownloadProgress |
| 130 | + |
| 131 | +**Description:** Emitted on download progress. |
| 132 | + |
| 133 | +### OnUpdateDownloaded |
| 134 | + |
| 135 | +**Description:** Emitted on download complete. |
| 136 | + |
| 137 | +## Usage Examples |
| 138 | + |
| 139 | +### Basic Auto-Update Setup |
| 140 | + |
| 141 | +```csharp |
| 142 | +// Configure auto-updater |
| 143 | +Electron.AutoUpdater.AutoDownload = true; |
| 144 | +Electron.AutoUpdater.AutoInstallOnAppQuit = true; |
| 145 | + |
| 146 | +// Check for updates |
| 147 | +var updateCheck = await Electron.AutoUpdater.CheckForUpdatesAsync(); |
| 148 | +if (updateCheck.UpdateInfo != null) |
| 149 | +{ |
| 150 | + Console.WriteLine($"Update available: {updateCheck.UpdateInfo.Version}"); |
| 151 | +} |
| 152 | +``` |
| 153 | + |
| 154 | +### Manual Update Management |
| 155 | + |
| 156 | +```csharp |
| 157 | +// Disable auto-download for manual control |
| 158 | +Electron.AutoUpdater.AutoDownload = false; |
| 159 | + |
| 160 | +// Check for updates |
| 161 | +var result = await Electron.AutoUpdater.CheckForUpdatesAsync(); |
| 162 | +if (result.UpdateInfo != null) |
| 163 | +{ |
| 164 | + Console.WriteLine($"Update found: {result.UpdateInfo.Version}"); |
| 165 | + |
| 166 | + // Ask user confirmation |
| 167 | + var confirmResult = await Electron.Dialog.ShowMessageBoxAsync( |
| 168 | + "Update Available", |
| 169 | + $"Version {result.UpdateInfo.Version} is available. Download now?"); |
| 170 | + |
| 171 | + if (confirmResult.Response == 1) // Yes |
| 172 | + { |
| 173 | + // Download update manually |
| 174 | + var downloadPath = await Electron.AutoUpdater.DownloadUpdateAsync(); |
| 175 | + Console.WriteLine($"Downloaded to: {downloadPath}"); |
| 176 | + |
| 177 | + // Install update |
| 178 | + Electron.AutoUpdater.QuitAndInstall(); |
| 179 | + } |
| 180 | +} |
| 181 | +``` |
| 182 | + |
| 183 | +### Update Event Handling |
| 184 | + |
| 185 | +```csharp |
| 186 | +// Handle update events |
| 187 | +Electron.AutoUpdater.OnCheckingForUpdate += () => |
| 188 | +{ |
| 189 | + Console.WriteLine("Checking for updates..."); |
| 190 | +}; |
| 191 | + |
| 192 | +Electron.AutoUpdater.OnUpdateAvailable += (updateInfo) => |
| 193 | +{ |
| 194 | + Console.WriteLine($"Update available: {updateInfo.Version}"); |
| 195 | +}; |
| 196 | + |
| 197 | +Electron.AutoUpdater.OnUpdateNotAvailable += (updateInfo) => |
| 198 | +{ |
| 199 | + Console.WriteLine("No updates available"); |
| 200 | +}; |
| 201 | + |
| 202 | +Electron.AutoUpdater.OnDownloadProgress += (progressInfo) => |
| 203 | +{ |
| 204 | + Console.WriteLine($"Download progress: {progressInfo.Percent}%"); |
| 205 | +}; |
| 206 | + |
| 207 | +Electron.AutoUpdater.OnUpdateDownloaded += (updateInfo) => |
| 208 | +{ |
| 209 | + Console.WriteLine($"Update downloaded: {updateInfo.Version}"); |
| 210 | + |
| 211 | + // Show notification to user |
| 212 | + Electron.Notification.Show(new NotificationOptions |
| 213 | + { |
| 214 | + Title = "Update Downloaded", |
| 215 | + Body = $"Version {updateInfo.Version} is ready to install.", |
| 216 | + Actions = new[] |
| 217 | + { |
| 218 | + new NotificationAction { Text = "Install Now", Type = NotificationActionType.Button }, |
| 219 | + new NotificationAction { Text = "Later", Type = NotificationActionType.Button } |
| 220 | + } |
| 221 | + }); |
| 222 | +}; |
| 223 | + |
| 224 | +Electron.AutoUpdater.OnError += (error) => |
| 225 | +{ |
| 226 | + Console.WriteLine($"Update error: {error}"); |
| 227 | + Electron.Dialog.ShowErrorBox("Update Error", $"Failed to update: {error}"); |
| 228 | +}; |
| 229 | +``` |
| 230 | + |
| 231 | +### GitHub Provider Configuration |
| 232 | + |
| 233 | +```csharp |
| 234 | +// Configure for GitHub releases |
| 235 | +Electron.AutoUpdater.AllowPrerelease = true; // Allow pre-release versions |
| 236 | +Electron.AutoUpdater.FullChangelog = true; // Get full changelog |
| 237 | +Electron.AutoUpdater.AllowDowngrade = false; // Prevent downgrades |
| 238 | +
|
| 239 | +// Set request headers if needed |
| 240 | +Electron.AutoUpdater.RequestHeaders = new Dictionary<string, string> |
| 241 | +{ |
| 242 | + ["Authorization"] = "token your-github-token", |
| 243 | + ["User-Agent"] = "MyApp-Updater" |
| 244 | +}; |
| 245 | +``` |
| 246 | + |
| 247 | +### Update Installation |
| 248 | + |
| 249 | +```csharp |
| 250 | +// Install update immediately |
| 251 | +if (updateDownloaded) |
| 252 | +{ |
| 253 | + Electron.AutoUpdater.QuitAndInstall(); |
| 254 | +} |
| 255 | + |
| 256 | +// Silent install (Windows only) |
| 257 | +Electron.AutoUpdater.QuitAndInstall(isSilent: true, isForceRunAfter: true); |
| 258 | +``` |
| 259 | + |
| 260 | +### Version Management |
| 261 | + |
| 262 | +```csharp |
| 263 | +// Get current version |
| 264 | +var currentVersion = await Electron.AutoUpdater.CurrentVersionAsync; |
| 265 | +Console.WriteLine($"Current version: {currentVersion}"); |
| 266 | + |
| 267 | +// Get update channel |
| 268 | +var channel = await Electron.AutoUpdater.ChannelAsync; |
| 269 | +Console.WriteLine($"Update channel: {channel}"); |
| 270 | + |
| 271 | +// Set custom feed URL |
| 272 | +// Note: This would typically be configured in electron-builder.json |
| 273 | +var feedUrl = await Electron.AutoUpdater.GetFeedURLAsync(); |
| 274 | +Console.WriteLine($"Feed URL: {feedUrl}"); |
| 275 | +``` |
| 276 | + |
| 277 | +## Related APIs |
| 278 | + |
| 279 | +- [Electron.App](App.md) - Application lifecycle events during updates |
| 280 | +- [Electron.Notification](Notification.md) - Notify users about update status |
| 281 | +- [Electron.Dialog](Dialog.md) - Show update confirmation dialogs |
| 282 | + |
| 283 | +## Additional Resources |
| 284 | + |
| 285 | +- [Electron AutoUpdater Documentation](https://electronjs.org/docs/api/auto-updater) - Official Electron auto-updater API |
0 commit comments