以编程方式更新 ClickOnce 部署 (VSTO) 会导致 ApplicationDeployment.IsNetw



我有一个 Excel VSTO 加载项,它通过 ClickOnce 每 24 小时更新一次。这工作正常。

我想提供一个按钮,用户可以立即手动检查更新。我按照文档中提供的说明进行操作。我的代码如下所示:(暂时忽略注释部分(

Sub TryUpdateApp()
If (ApplicationDeployment.IsNetworkDeployed) Then
Dim Deployment As ApplicationDeployment = ApplicationDeployment.CurrentDeployment
Dim Info As UpdateCheckInfo = Nothing
'Try
'    Dim AppIdentity As New ApplicationIdentity(Deployment.UpdatedApplicationFullName)
'    Dim UnrestrictedPerms As New Security.PermissionSet(Security.Permissions.PermissionState.Unrestricted)
'    Dim AppTrust As New Security.Policy.ApplicationTrust(AppIdentity) With {
'          .DefaultGrantSet = New Security.Policy.PolicyStatement(UnrestrictedPerms),
'          .IsApplicationTrustedToRun = True,
'          .Persist = True
'          }
'    Security.Policy.ApplicationSecurityManager.UserApplicationTrusts.Add(AppTrust)
'Catch ex As Exception
'    'log error
'End Try
Try
Info = Deployment.CheckForDetailedUpdate()
Catch dde As DeploymentDownloadException
MsgBox($"The new version of App cannot be downloaded at this time.{vbNewLine}Please check your network connection, or try again later. Error: {dde.Message}", vbExclamation Or vbOKOnly)
Exit Sub
Catch ioe As InvalidOperationException
MsgBox($"This application cannot be updated. It is likely not a ClickOnce application. Error: {ioe.Message}", vbCritical Or vbOKOnly)
Exit Sub
End Try
Try
If (Info.UpdateAvailable) Then
Try
Deployment.Update()
MsgBox("App has been upgraded. Please restart Excel to apply changes.", vbInformation Or vbOKOnly)
Catch dde As DeploymentDownloadException
MsgBox($"Unable to install the latest version of App: download failed.{vbNewLine}Please check your network connection, or try again later.", vbCritical Or vbOKOnly)
Exit Sub
Catch tnge As TrustNotGrantedException
MsgBox("Unable to install the latest version of App: trust not granted.", vbExclamation Or vbOKOnly)
Exit Sub
End Try
Else
MsgBox("The latest version of App is already installed.", vbInformation Or vbOKOnly)
End If
Catch ex As Exception
MsgBox("Unable to install the latest version of App: unknown error.")
Exit Sub
End Try
Else
Throw New ApplicationException("Application is not network deployed.")
End If
End Sub

虽然它会准确地指示"已安装最新版本的应用程序",但如有必要,它将无法更新,从而引发TrustNotGrantedException: User has refused to grant required permissions to the application.

第一个有趣的事情是,此异常是由"无法安装最新版本的应用程序:未知错误"捕获的,而不是"无法安装最新版本的应用程序:未授予信任",正如人们所期望的那样。

我找到了这个线程,它对应于上面代码的注释部分。当我取消评论并运行 sub 时,它似乎可以工作,因为我得到"应用程序已升级。请重新启动 Excel 以应用更改"。但是,当我重新启动 Excel 并再次运行 Sub 时,我得到"应用程序未部署网络"。

我该如何解决这个问题?(任何 C# 代码都可以(

首先,尝试在受信任站点列表中添加应用程序 URL。

根据 MSDN,如果出现以下情况,则会抛出TrustNotGrantedException

应用程序使用权限提升,用户拒绝提升信任的请求;或

应用程序使用受信任的应用程序部署,用于对应用程序进行签名的数字证书未在本地计算机上列为受信任的发布者。如果已将更新部署到应用程序,并且该更新使用的权限比以前的版本多,并且 ClickOnce 引发 TrustNotGrantedException,则不会安装新版本。

由于这是一个完全信任的应用程序,您的用户是否具有管理员权限,是否有任何操作需要管理员权限?

您的证书是来自 CA,还是由 VS 生成的测试证书?如果这是测试证书,则需要检查它是否已添加到"信任发布者"列表中。

有关更多信息,请参见如何:向 ClickOnce 应用程序的客户端计算机添加受信任的发布者。

最新更新