0
点赞
收藏
分享

微信扫一扫

C# 客户端跟服务器自动更新功能项目2

at小涛 2022-01-05 阅读 78

自动更新需要自己编写一个更新的exe

例如:autorunFrm.exe

如果直接用引用组件DLL更新文件的方式存在一个问题,无法更新主程序EXE。所以要更新自带的exe主程序需要一个更新的exe

具体代码如下:

autorunFrm里面的更新代码:

    /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                #region check and download new version program
                bool bHasError = false;
                AutoUpdater.AutoUpdater autoUpdater = new AutoUpdater.AutoUpdater();
                try
                {
                    autoUpdater.Update();
                }
                catch (WebException exp)
                {
                    MessageBox.Show(string.Format("找不到指定的资源,请检查升级地址:{0}是否正确,确保网络通顺!", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConstFile.FILENAME)));
                    bHasError = true;
                }
                catch (XmlException exp)
                {
                    bHasError = true;
                    MessageBox.Show("下载升级文件时出错,请检查升级文件是否正确!");
                }
                catch (NotSupportedException exp)
                {
                    bHasError = true;
                    MessageBox.Show("升级地址配置错误");
                }
                catch (ArgumentException exp)
                {
                    bHasError = true;
                    MessageBox.Show("下载升级文件时出错");
                }
                catch (Exception exp)
                {
                    bHasError = true;
                    MessageBox.Show("升级过程中发生错误");
                }
                finally
                {
                    if (bHasError == true)
                    {
                        try
                        {
                            autoUpdater.RollBack();
                        }
                        catch (Exception)
                        {
                            //Log the message to your file or database
                        }
                    }
                }
                #endregion
                //  Application.Run(autoUpdater);
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.ToString());
            }
        }

具体更新代码参考:

AutoUpdater迁移到Github - 圣殿骑士 - 博客园

举报

相关推荐

0 条评论