如何:使用 ClickOnce 部署 API 以编程方式检查应用程序更新
ClickOnce 提供了两种在部署后更新应用程序的方法。 在第一种方法中,可以将 ClickOnce 部署配置为以特定间隔时间自动检查是否有更新。 在第二种方法中,可以编写使用 ApplicationDeployment 类根据事件(如用户请求)检查是否有更新的代码。
下面的过程演示一些执行编程更新的代码,同时描述如何配置 ClickOnce 部署,以启用编程更新检查。
为了以编程方式更新 ClickOnce 应用程序,必须为更新指定一个位置。 这有时称为部署提供程序。 有关设置此属性的更多信息,请参见选择 ClickOnce 更新策略。
提示
也可以使用下面所描述的技术在一个位置部署应用程序,而从其他位置更新该应用程序。 有关更多信息,请参见如何:指定部署更新的其他位置。
以编程方式检查更新
使用您喜爱的命令行或可视化工具创建一个新的 Windows 窗体应用程序。
创建希望让用户选择以检查是否有更新的任何按钮、菜单项或其他用户界面项。 从该项的事件处理程序调用下列方法以检查是否有更新并安装更新。
Private Sub InstallUpdateSyncWithInfo() Dim info As UpdateCheckInfo = Nothing If (ApplicationDeployment.IsNetworkDeployed) Then Dim AD As ApplicationDeployment = ApplicationDeployment.CurrentDeployment Try info = AD.CheckForDetailedUpdate() Catch dde As DeploymentDownloadException MessageBox.Show("The new version of the application cannot be downloaded at this time. " + ControlChars.Lf & ControlChars.Lf & "Please check your network connection, or try again later. Error: " + dde.Message) Return Catch ioe As InvalidOperationException MessageBox.Show("This application cannot be updated. It is likely not a ClickOnce application. Error: " & ioe.Message) Return End Try If (info.UpdateAvailable) Then Dim doUpdate As Boolean = True If (Not info.IsUpdateRequired) Then Dim dr As DialogResult = MessageBox.Show("An update is available. Would you like to update the application now?", "Update Available", MessageBoxButtons.OKCancel) If (Not System.Windows.Forms.DialogResult.OK = dr) Then doUpdate = False End If Else ' Display a message that the app MUST reboot. Display the minimum required version. MessageBox.Show("This application has detected a mandatory update from your current " & _ "version to version " & info.MinimumRequiredVersion.ToString() & _ ". The application will now install the update and restart.", _ "Update Available", MessageBoxButtons.OK, _ MessageBoxIcon.Information) End If If (doUpdate) Then Try AD.Update() MessageBox.Show("The application has been upgraded, and will now restart.") Application.Restart() Catch dde As DeploymentDownloadException MessageBox.Show("Cannot install the latest version of the application. " & ControlChars.Lf & ControlChars.Lf & "Please check your network connection, or try again later.") Return End Try End If End If End If End Sub
private void InstallUpdateSyncWithInfo() { UpdateCheckInfo info = null; if (ApplicationDeployment.IsNetworkDeployed) { ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment; try { info = ad.CheckForDetailedUpdate(); } catch (DeploymentDownloadException dde) { MessageBox.Show("The new version of the application cannot be downloaded at this time. \n\nPlease check your network connection, or try again later. Error: " + dde.Message); return; } catch (InvalidDeploymentException ide) { MessageBox.Show("Cannot check for a new version of the application. The ClickOnce deployment is corrupt. Please redeploy the application and try again. Error: " + ide.Message); return; } catch (InvalidOperationException ioe) { MessageBox.Show("This application cannot be updated. It is likely not a ClickOnce application. Error: " + ioe.Message); return; } if (info.UpdateAvailable) { Boolean doUpdate = true; if (!info.IsUpdateRequired) { DialogResult dr = MessageBox.Show("An update is available. Would you like to update the application now?", "Update Available", MessageBoxButtons.OKCancel); if (!(DialogResult.OK == dr)) { doUpdate = false; } } else { // Display a message that the app MUST reboot. Display the minimum required version. MessageBox.Show("This application has detected a mandatory update from your current " + "version to version " + info.MinimumRequiredVersion.ToString() + ". The application will now install the update and restart.", "Update Available", MessageBoxButtons.OK, MessageBoxIcon.Information); } if (doUpdate) { try { ad.Update(); MessageBox.Show("The application has been upgraded, and will now restart."); Application.Restart(); } catch (DeploymentDownloadException dde) { MessageBox.Show("Cannot install the latest version of the application. \n\nPlease check your network connection, or try again later. Error: " + dde); return; } } } } }
public: void InstallUpdateSync() { if (ApplicationDeployment::IsNetworkDeployed) { bool isUpdateAvailable = false; ApplicationDeployment^ appDeployment = ApplicationDeployment::CurrentDeployment; try { isUpdateAvailable = appDeployment->CheckForUpdate(); } catch (InvalidOperationException^ ex) { MessageBox::Show("The update check failed. Error: {0}", ex->Message); return; } if (isUpdateAvailable) { try { appDeployment->Update(); MessageBox::Show( "The application has been upgraded, and will now " + "restart."); Application::Restart(); } catch (Exception^ ex) { MessageBox::Show("The update failed. Error: {0}", ex->Message); return; } } } }
编译应用程序。
使用 Mage.exe 部署以编程方式检查是否有更新的应用程序
按照 演练:手动部署 ClickOnce 应用程序 中描述的使用 Mage.exe 部署应用程序的说明进行操作。 在调用 Mage.exe 生成部署清单时,确保使用命令行开关 providerUrl 及指定 ClickOnce 应检查是否有更新的 URL。 例如,如果应用程序将从 http://www.adatum.com/MyApp 进行更新,则生成部署清单的调用可能如下所示:
mage -New Deployment -ToFile WindowsFormsApp1.application -Name "My App 1.0" -Version 1.0.0.0 -AppManifest 1.0.0.0\MyApp.manifest -providerUrl http://www.adatum.com/MyApp/MyApp.application
使用 MageUI.exe 部署以编程方式检查是否有更新的应用程序
- 按照 演练:手动部署 ClickOnce 应用程序 中描述的使用 Mage.exe 部署应用程序的说明进行操作。 在**“部署选项”选项卡上,将“启动位置”字段设置为 ClickOnce 应检查是否有更新的应用程序清单。 在“更新选项”选项卡上,清除“此应用程序应该检查更新”**复选框。
安全性
若要使用编程更新,则您的应用程序必须具有完全信任权限。