c# - 从 Office 本身更新 ClickOnce VSTO 加载项不会更新加载项

标签 c# .net ms-office vsto add-in

我在功能区上有一个按钮来检查 AddIn(本身)更新

这是代码

private void button1_Click(object sender, RibbonControlEventArgs e)
{
    UpdateCheckInfo info = null;

    if (ApplicationDeployment.IsNetworkDeployed)
    {
        ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;
        var appId = new ApplicationIdentity(ad.UpdatedApplicationFullName);
        var unrestrictedPerms = new PermissionSet(PermissionState.Unrestricted);
        var appTrust = new ApplicationTrust(appId)
        {
            DefaultGrantSet = new PolicyStatement(unrestrictedPerms),
            IsApplicationTrustedToRun = true,
            Persist = true
        };

        ApplicationSecurityManager.UserApplicationTrusts.Add(appTrust);

        info = ad.CheckForDetailedUpdate();

        if (info.UpdateAvailable)
        {
            ad.Update();
            MessageBox.Show("DONE");
        }
    }
}

发生的事情是我收到“完成”消息框,但在重新启动 Excel 后,插件实际上没有更新并且我无法再次更新它,因为下次我单击相同的按钮,ApplicationDeployment.IsNetworkDeployed 返回 false

我该如何解决这个问题?

最佳答案

我相信可以在这篇 MSDN 帖子中找到答案:VSTO, ClickOnce and auto update

节选:

This is True: VSTO applications are ClickOnce applications

This is not True: The ClickOnce API is supported by VSTO applications Why: While VSTO Applications are ClickOnce applications, they require functionality that extends the base implementation of ClickOnce. A product of this requirement is that not everything within ClickOnce (for Windows Forms) applies to VSTO. One of these specific areas is the Runtime API.

This is True: Some parts of the API will work Why: Because the VSTO Runtime Uses the core part of ClickOnce, some parts actually will function. What is not known is where exactly this line is drawn. I have found as very loose general rule of thumb: anything that doesn't change the state of the application (anything that provides you with "information") will likely work. This is why my blog post describes how to use the API to "check" for the update but uses the VSTOInstaller exe to do the actual act of updating.

This is not True: You can use the API to Download an update Why: This goes back to the ClickOnce/VSTO difference. If you imagine ClickOnce as this sort of generic technology, you can think of VSTO as a specific implementation of it. In most cases (specifically Winforms applications) the generic technology does everything that is required. For VSTO though, we needed to extend the technology to make it do stuff it had never done before (specifically register customizations with office and maintain some data need to setup entrypoints and whatnot). As such the generic technology doesn't provide all of the functionality we need. In this specific case incurring an update changes the state of the application in such a way that we have to change some of the registration information for Office. ClickOnce "doesn know" enough to update these values, and as such isn't capable (in its current state) of doing a "correct" update of a VSTO application. It is the VSTO Runtime that does these steps.

他提到了一篇博文,我相信是这篇:Click-Once forced updates in VSTO: Some things we don’t recommend using, that you might consider anyway.

节选:

//Call VSTOInstaller Explicitely in "Silent Mode"
string installerArgs = " /S /I \\\\GenericServer\\WordDocument2.vsto";
string installerPath = "C:\\Program Files\\Common Files\\microsoft 
shared\\VSTO\\9.0\\VSTOINSTALLER.exe";

System.Diagnostics.Process VstoInstallerProc = new System.Diagnostics.Process();
VstoInstallerProc.StartInfo.Arguments = installerArgs;
VstoInstallerProc.StartInfo.FileName = installerPath;
VstoInstallerProc.Start();
VstoInstallerProc.WaitForExit();

它不完全是生产就绪代码,但您明白了。

关于c# - 从 Office 本身更新 ClickOnce VSTO 加载项不会更新加载项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39619047/

相关文章:

delphi - "old format or invalid type library"

c# - 我需要 SQLBulkcopy 的进度条

c# - Linq 性能 : should I first use `where` or `select`

C# 改变按钮文本的大小

c# - 试图重现 "must declare a body"编译器错误

ms-office - 我们应该依赖 Microsoft Office 组件吗?

javascript - 如何在独立脚本中使用 Microsoft Office 2013 javascript API

c# - 在 C# 中增加 WCF Web 服务的超时

c# - C# 中的 "Application Configuration File"和 "Settings File"有什么区别?

c# - Dispose 如何与 Entity Framework 配合使用