c#自动更新-源码

软件维护与升级

  • 修复漏洞和缺陷:软件在使用过程中可能会发现各种漏洞和缺陷,自动更新可以及时推送修复程序,增强软件的稳定性和安全性,避免因漏洞被利用而导致数据泄露、系统崩溃等问题。

  • 提升性能:通过自动更新,可以对软件的算法、代码逻辑等进行优化,提高软件的运行效率,减少资源占用,让软件运行得更加流畅。

  • 添加新功能:随着业务的发展和用户需求的变化,软件需要不断添加新功能来满足用户。自动更新能够方便地将新功能推送给用户,无需用户手动下载和安装新版本,提升用户体验。

    ///


    /// 检测更新
    ///

    private void checkUpdate()
    {

    复制代码
              strUpdateURL = getConfigValue(strUpdateXmlPath, "Url");     //读取本地xml中配置的更新服务器的URL
              string strLastUpdateDate = getConfigValue(strUpdateXmlPath, "UpDate");   //读取本地xml中配置的最近一次更新日期
    
              if (strUpdateURL.Substring(strUpdateURL.Length - 1) != "/")       //如果配置的xml中URL没带最后一个反斜杠,则加一下,防止出错
                  strUpdateURL += "/";
    
              strTheUpdateDate = getTheLastUpdateTime(strUpdateURL);        //获得更新服务器端的此次更新日期
              if (!String.IsNullOrEmpty(strTheUpdateDate) && !String.IsNullOrEmpty(strLastUpdateDate))      //日期都不为空
              {
                  if (DateTime.Compare(
                      Convert.ToDateTime(strTheUpdateDate, CultureInfo.InvariantCulture),
                      Convert.ToDateTime(strLastUpdateDate, CultureInfo.InvariantCulture)) > 0)     //字符转日期,并比较日期大小
                  {
                      //本次更新日期 大于 最近一次更新日期,开始更新
                      try
                      {
                          if (new K3SP.lib.ClassCheckProIsRun().checkProcess(strUpdaterProFileName, strUpdaterProPath))
                          {
                              classMsg.messageInfoBox("更新程序" + strUpdaterProFileName + "已打开!");
                          }
                          else
                          {
                              Process.Start(strUpdaterProPath);
                          }
                      }
                      catch (Win32Exception ex)
                      {
                          classMsg.messageInfoBox(ex.Message);      //主程序未更新成功或者被误删掉,再更新一遍
                      }
                      Application.Exit();         //退出主程序
                  }
              }
          }
    
          /// <summary> 
          /// 读取本地update.xml 
          /// </summary> 
          /// <param name="path">update.xml文件的路径</param> 
          /// <param name="appKey">"key"的值</param> 
          /// <returns>返回"value"的值</returns> 
          internal static string getConfigValue(string path, string appKey)
          {
              XmlDocument xDoc = new XmlDocument();
              XmlNode xNode;
              XmlElement xElem = null;
              try
              {
                  xDoc.Load(path);
    
                  xNode = xDoc.SelectSingleNode("//appSettings");
    
                  xElem = (XmlElement)xNode.SelectSingleNode("//add[@key=\"" + appKey + "\"]");
    
              }
              catch (XmlException ex)
              {
                  classMsg.messageInfoBox(ex.Message);
              }
              if (xElem != null)
                  return xElem.GetAttribute("value");
              else
                  return "";
          }
    
          /// <summary> 
          /// 获取服务器端软件的更新日期 
          /// </summary> 
          /// <param name="Dir">服务器地址</param>
          /// <returns>返回日期</returns> 
          private static string getTheLastUpdateTime(string Dir)
          {
              string LastUpdateTime = "";
              string AutoUpdaterFileName = Dir + strUpdateListXmlPath;
              try
              {
                  WebClient wc = new WebClient();
                  Stream sm = wc.OpenRead(AutoUpdaterFileName);
                  XmlTextReader xml = new XmlTextReader(sm);
                  while (xml.Read())
                  {
                      if (xml.Name == "UpdateTime")
                      {
                          LastUpdateTime = xml.GetAttribute("Date");
                          break;
                      }
                  }
                  xml.Close();
                  sm.Close();
              }
              catch (WebException ex)
              {
                  classMsg.messageInfoBox(ex.Message);
              }
              return LastUpdateTime;
          }
      }

变量信息

复制代码
        /// <summary>
        /// 全局变量,用于存储更新服务器的URL
        /// </summary>
        private static string strUpdateURL;

        /// <summary>
        /// 本地update.xml的路径
        /// </summary>
        private static string strUpdateXmlPath = Application.StartupPath + @"\update\conf\update.xml";

        /// <summary>
        /// 服务端updatelist.xml的URL地址 
        /// </summary>
        private static string strUpdateListXmlPath = "UpdateServer/UpdateList.xml";

        /// <summary>
        /// 全局变量,用于存储服务端updatelist.xml的更新日期 
        /// </summary>
        private static string strTheUpdateDate;

        /// <summary>
        /// 更新程序的文件名
        /// </summary>
        private static string strUpdaterProFileName = "AutoUpdater";

        /// <summary>
        /// 更新程序的路径
        /// </summary>
        private static string strUpdaterProPath = Application.StartupPath + @"\update\AutoUpdater.exe";

程序启动

复制代码
        private void FormLogin_Load(object sender, EventArgs e)
        {
            checkUpdate();  //检测更新
        }

        /// <summary>
        /// 进入程序
        /// </summary>
        private void button_Login_Click(object sender, EventArgs e)
        {
            FormMain form_Main = new FormMain();
            form_Main.Show();
            this.Hide();
        }
相关推荐
Novproxy_Z8 小时前
商用代理避坑:如何识别商家拿机房 IP 冒充住宅 IP
java·网络·tcp/ip
无足鸟ICT8 小时前
【RHCA+】vmstat命令(监控系统资源)
linux·服务器
垆边人似月.9 小时前
RDMA 文件传输踩坑(五):READY 发送成功后,为什么出现 Work Request Flushed Error?
服务器·网络·tcp/ip
闪亮的路灯9 小时前
Linux 安装mqtt服务mosquitto
linux·运维·服务器
HideF9 小时前
nmcli
linux·服务器·网络
qetfw10 小时前
UOS DHCP Relay:跨网段分配与 IP、DNS、网关验证
linux·服务器·网络·tcp/ip·uos
尹人入圣10 小时前
目前热门的IP驱动产业新场景新工具
网络·python·网络协议·tcp/ip
明航咨询-陈老师10 小时前
ITSS运行维护服务能力成熟度认证技术纵深:标准模型、等级路径、2026审核趋势与CS/CMMI对比
服务器·数据库·cmmi
中屹指纹浏览器svip11 小时前
只用指纹浏览器,不搭配IP可以吗?
网络
jimmyleeee11 小时前
Agent安全之八:OWASP ASI08 级联失效------当每一个 Agent 都“正确”,系统却走向灾难
网络·安全·web安全