关于c#:VSTO部署方案

72次阅读

共计 2718 个字符,预计需要花费 7 分钟才能阅读完成。

VSTO 部署计划

背景:须要应用 VSTO 开发一个 Excel 插件,插件具备自动更新的性能
VisualStudio: 2022
针对 Excel 版本:2016

ClickOnce部署

该计划应用了一个文件服务器用来拜访部署文件。(后以 www.example.com/Files 作为示例地址)

  1. 应用 VS 模板,创立一个 Excel AddIn 我的项目(在其中填充你想要的性能,此处略过不提)
  2. 右键我的项目,关上属性界面
  3. 选中 公布 页签,进行设置并点击公布
  4. 公布后的文件复制到文件服务器的目录下即可
  5. 用户从对应 URL 下载 Setup.exe 进行装置即可;后续也会主动从对应 URL 拉取更新的

问题

  1. 如果呈现 从不信赖的地位或起源装置 的报错,导致无奈进行装置,能够间接采纳信赖证书的形式装置
    右键下载下来的Setup.exe,找到证书进行装置

    起因:公布的软件都有证书,在属性界面的签名页签里进行设置。默认应用的是测试证书,用户机器不信赖。须要手动装置信赖下才能够失常。
    后续不更改证书,就不必从新信赖了

dll部署

ClickOnce部署很不便,然而没有版本倒退的性能。在理论的我的项目中,插件 版本往往和 数据 版本有关联的,切换到指定的 svngit版本,心愿也能有对应版本的插件性能。
svn 版本控制为例,阐明怎么应用 dll 部署。

插件本体我的项目

依然采纳 ClickOnce 部署的形式,不过能够不填 近程 Url,因为不存在插件更新的状况;界面及性能采纳 Dll 的形式书写

Dll 我的项目创立

  1. 创立一个 Net Framework 类库
  2. 引入相干援用;将失常的 VSTO 我的项目中的援用都增加进来
  3. 创立绘制的函数(从 VSTO 设计器产生的函数间接复制过去就行)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Office.Tools.Ribbon;
    namespace ClassLibrary1
    {
    public static class Class1
    {
    /// <summary>
    /// 绘制 tab,返回提供给 VSTO 应用
    /// </summary>
    /// <param name="factory"></param>
    /// <returns></returns>
    public static RibbonTab DrawTab(RibbonFactory factory)
    {RibbonTab tab1 = factory.CreateRibbonTab();
    RibbonGroup group1 = factory.CreateRibbonGroup();
    tab1.SuspendLayout();
    tab1.ControlId.ControlIdType = Microsoft.Office.Tools.Ribbon.RibbonControlIdType.Office;
    tab1.Groups.Add(group1);
    tab1.Label = "TabAddIns";
    tab1.Name = "tab1";
    group1.Label = "group1";
    group1.Name = "group1";
    tab1.ResumeLayout(false);
    tab1.PerformLayout();
    return tab1;
    }
    }
    }
  4. VSTO我的项目中的功能区代码略作调整
    设计器产生代码
    批改前

    private void InitializeComponent()
    {this.tab1 = this.Factory.CreateRibbonTab();
    this.group1 = this.Factory.CreateRibbonGroup();
    this.tab1.SuspendLayout();
    this.SuspendLayout();
    //
    // tab1
    //
    this.tab1.ControlId.ControlIdType = Microsoft.Office.Tools.Ribbon.RibbonControlIdType.Office;
    this.tab1.Groups.Add(this.group1);
    this.tab1.Label = "TabAddIns";
    this.tab1.Name = "tab1";
    //
    // group1
    //
    this.group1.Label = "group1";
    this.group1.Name = "group1";
    //
    // Ribbon1
    //
    this.Name = "Ribbon1";
    this.RibbonType = "Microsoft.Excel.Workbook";
    this.Tabs.Add(this.tab1);
    this.Load += new Microsoft.Office.Tools.Ribbon.RibbonUIEventHandler(this.Ribbon1_Load);
    this.tab1.ResumeLayout(false);
    this.tab1.PerformLayout();
    this.ResumeLayout(false);
    }

    批改后

    private void InitializeComponent()
    {this.SuspendLayout();
    // 依据 dll 获取函数,产生 tab
    Assembly assembly = Assembly.LoadFrom("XXX/test.dll");
    Type type = assembly.GetType("ClassLibrary1.Class1");
    MethodInfo methodInfo = type.GetMethod("DrawTab",
    BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static
    );
    this.tab1 = (RibbonTab)methodInfo.Invoke(null, new[] {this.Factory});
    this.Name = "Ribbon1";
    this.RibbonType = "Microsoft.Excel.Workbook";
    this.Tabs.Add(this.tab1);
    this.ResumeLayout(false);
    }

这样在关上插件之后,会依据 dll 主动获取绘制函数,从而实现依据 dll 的内容更新插件。

问题

  1. 不同用户,机子上 dll 的门路可能不同,怎么获取?
    能够要求用户首次应用的时候,执行一个 .bat,让用户输出本人的svn 我的项目门路,而后依据 svn 门路和 相对路径 ,拼出来dll 的门路。

正文完
 0