public Task<bool> DownloadUpdate(VersionInfo versionInfo, string savePath) { return Task.Factory.StartNew(() => { // Store to a temp file while downloading string tempPath = Path.Combine(Paths.Temp, Path.GetRandomFileName()); urlFileDownloader.DownloadFile(versionInfo.DownloadUrl, tempPath); // Handle the somewhat tricky situation where the file to save to already exists if (File.Exists(savePath)) { // Try overwriting the file, which should work if it isn't locked by another process try { File.Delete(savePath); } catch (IOException) { File.Delete(tempPath); return false; } } // Now that the download is complete, rename/move the temp file File.Move(tempPath, savePath); return true; }); }
public void PerformUpdate(IAutoUpdaterClient client, VersionInfo versionInfo) { switch (formFactory.Create<FUpdate>().ShowDialog()) { case DialogResult.Yes: // Install // TODO: The app process might need to be killed/restarted before/after installing autoUpdater.DownloadAndInstallUpdate(versionInfo).ContinueWith(result => { if (result.Result) { client.InstallComplete(); } else { MessageBox.Show(MiscResources.InstallFailed, MiscResources.InstallFailedTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); } }); break; case DialogResult.OK: // Download var saveDialog = new SaveFileDialog { FileName = versionInfo.FileName }; if (saveDialog.ShowDialog() == DialogResult.OK) { // TODO: Display progress while downloading autoUpdater.DownloadUpdate(versionInfo, saveDialog.FileName); } break; } }
public Task<bool> DownloadAndInstallUpdate(VersionInfo versionInfo) { return Task.Factory.StartNew(() => { var saveFolder = Path.Combine(Paths.Temp, Path.GetRandomFileName()); var savePath = Path.Combine(saveFolder, versionInfo.FileName); Directory.CreateDirectory(saveFolder); try { if (!DownloadUpdate(versionInfo, savePath).Result) { return false; } if (!InstallUpdate(savePath, versionInfo.InstallArguments).Result) { return false; } return true; } finally { Directory.Delete(saveFolder, true); } }); }
public void UpdateAvailable(VersionInfo versionInfo) { Invoke(() => autoUpdaterUI.PerformUpdate(this, versionInfo)); }