【发布时间】:2020-05-21 14:06:23
【问题描述】:
我有一个 Excel VSTO 加载项,它通过 ClickOnce 每 24 小时更新一次。这很好用。
我想提供一个按钮,用户可以在其中立即手动检查更新。我关注了instructions provided in the documentation。我的代码如下所示:(暂时忽略注释部分)
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.。
第一个有趣的事情是,这个异常是由“无法安装最新版本的应用程序:未知错误。”捕获的,而不是“无法安装最新版本的应用程序:未授予信任。”,正如人们所期望的那样.
然后找到this thread,对应上面代码的注释部分。当我取消注释并运行子程序时,它似乎可以正常工作,因为我得到“应用程序已升级。请重新启动 Excel 以应用更改”。但是,当我重新启动 Excel 并再次运行 Sub 时,我得到“应用程序未部署网络”。
我该如何解决这个问题? (任何 C# 代码都可以)
【问题讨论】:
标签: .net vb.net vsto clickonce office-addins