Office插件—c#自动升级(二)

1,360 阅读3分钟

一、前言

大家好,你的月亮我的心,我是博主小阿金,欢迎各位工友。
接着上篇文章讲到,由于技术的变更博主被迫转到了C#开发办公软件插件的冷门赛道,经过领导日日夜夜的摧残,终于也算搞出了一点小名堂(附图) 企业微信截图_1715242135510.png 但是好景不长,刚休息一会儿,领导灵光一闪,又让做一个插件自动升级的功能,经过在下阅读了各方资料后,整理了一番自己的处理方案,请各位工友给出指导意见。

二、打包工具

在打包工具上阿金选择了传统且强大的打包工具——Advanced Installer
网上下载资源很多,这里就不赘述了,直接上图。 选择office加载项,然后选择自己打包对应的配置即可,此文章主要研究如何自动升级,其他有需要帮忙的可以私聊阿金,此处不在赘述。 企业微信截图_17152426697240.png 注意事项一:每次更新需要更新版本号 企业微信截图_17152429037822.png 注意事项二:记下产品升级代码,产品升级需要保证此处代码一致,应用会根据此代码进行覆盖升级

企业微信截图_17152430661534.png 注意事项三:如果让用户最小感知软件更新操作可勾选此选项

企业微信截图_17152431163212.png 注意事项四:自定义行为添加 UninstallAppxPackage操作 用来卸载旧版本exe应用 企业微信截图_17152432021104.png 到这里打包需要注意的事项就完成啦。下面简单说下业务实现逻辑吧。

二、业务逻辑

这里简单说下博主自己的处理逻辑(附码) 1、初始化安装本地固化版本号
2、调用接口查询新版本号与之匹配判断是否需要更新
3、下载新的版本到本机指定位置
4、下载完毕执行程序完成版本升级。

    private void NavigationRibbon_Load(object sender, RibbonUIEventArgs e)
    {
        string folderPath = @"D:\document\plugins";
        string filePath = Path.Combine(folderPath, "version.txt");
        string dataPath = Path.Combine(folderPath, "updatedate.txt");
        // 判断系统下是否存在文件夹 D:\document\plugins,如果不存在则创建此文件夹
        if (!Directory.Exists(folderPath))
        {
            Directory.CreateDirectory(folderPath);
        }

        // 判断此文件夹下是否存在 version.txt 文件,如果不存在则创建此文件
        if (!File.Exists(filePath))
        {
            File.Create(filePath).Close(); // 创建文件并关闭文件流
            File.WriteAllText(filePath, "1.0.0");
        }
        if (!File.Exists(dataPath))
        {
            File.Create(dataPath).Close(); // 创建文件并关闭文件流
           
        }
        string currentDate = DateTime.Now.ToString("yyyy-MM-dd");
        //获取远程版本号
        try
        {
            List<string> list = BaseUrl.downloadInfo();
            if(list.Count>0)
            {
                //调用接口 获取remote 
                string remote = list[1];
                string url = list[0];
                string local = File.ReadAllText(filePath);
                //获取当前是否弹出 
                string localDate = File.ReadAllText(dataPath);
                //如果为空 或者时间当期年月日不相同则 弹出窗口
                if (string.IsNullOrEmpty(localDate) || !localDate.Equals(currentDate))
                {
                    if (remote != local)
                    {
                        Form form = new Update(url, remote);
                        form.Show();
                    }
                }
            }
            
        }
        catch (Exception)
        {
           
        }
}




namespace easy_bid_plugin.othertools
{
    public partial class Update : Form
    {

        private string url;
        private string ver;
        public Update(string url, string ver)
        {
            InitializeComponent();
            this.url = url;
            this.ver = ver;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.TopMost = true;
        }

        private void UpdateButton1_Click(object sender, EventArgs e)
        {

            try
            {
                string folderPath = @"D:\document\package";
                string newPluginPath = Path.Combine(folderPath, "易中标插件.exe");
                // 判断是否存在文件夹 D:\document\package,如果不存在则创建
                if (!Directory.Exists(folderPath))
                {
                    Directory.CreateDirectory(folderPath);
                }
                else
                {
                    // 清空该文件夹下所有内容,保留文件夹
                    DirectoryInfo di = new DirectoryInfo(folderPath);
                    foreach (FileInfo file in di.GetFiles())
                    {
                        file.Delete();
                    }
                }
                //调用下载接口  
                using (WebClient webClient = new WebClient())
                {
                    // 下载文件
                    webClient.DownloadFile(url, newPluginPath);
                    // 运行下载的文件
                    Process.Start(newPluginPath);
                }
                //更新版本号 
                string plugins = @"D:\document\plugins";
                string filePath = Path.Combine(plugins, "version.txt");
                File.WriteAllText(filePath, ver);
                //更新时间
                update();
                //更新word加载项  关闭word
                MessageBox.Show("安装成功");
                foreach (Process process in Process.GetProcessesByName("WINWORD"))
                {
                    process.Kill();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("更新失败: " + ex.Message);
            }


        }

        private void UpdateButton2_Click(object sender, EventArgs e)
        {
            update();
            this.Close();
        }


        public void update()
        {
            string currentDate = DateTime.Now.ToString("yyyy-MM-dd");
            string folderPath = @"D:\document\plugins";
            string dataPath = Path.Combine(folderPath, "updatedate.txt");
            if (UpdateCheckBox1.Checked)
            {
                File.WriteAllText(dataPath, currentDate);
            }
        }
    }
}

三、总结

按照上述逻辑即可实现远程插件的自动升级功能,对于升级版本还有另外一种ClickOnce不需要编写代码逻辑的方法,后续等有空研究一下在给各位兄弟们分享。