【CS】客户端更新(二)——生成更新配置文件程序介绍

本文介绍了一种自动生成XML配置文件的方法,用于软件更新过程。文章详细展示了如何使用C#编程语言来创建符合特定格式要求的XML文件,包括定义文件结构、添加节点及属性等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、前言

      在上一篇博客中,小编向大家介绍了【CS】客户端更新(一)——更新程序文件方式,更新的内容都是写在配置文件中,自然而然我们不可能手动去写配置文件,在后期维护非常的不方便,下面小编就结合上一篇博客,把更新的配置文件的操作展示一下。

二、配置文件结构分析

      不同的程序有不同的配置文件,小编在项目中使用的配置文件的类型是*.xml文件。xml文件的最大的特点就是可以携带数据,使用方便。

      下面是小编使用的配置文件:

<?xml version="1.0" encoding="utf-8"?>
<AutoUpdater>
  <Updater>
    <Url>ftp://73.16.18.144/</Url>
  </Updater>
  <Application applicationId="ItemSoft">
    <EntryPoint>ItemSoft</EntryPoint>
    <Location>.</Location>
  </Application>
  <Files>
    <File Ver="fa6d7e00-0c64-4ccd-b1c2-c7289268e5a6" Name="123.mp4" />
    <File Ver="d4b52955-0712-4274-ac6b-64d7415ccbf3" Name="DESDecder.exe" />
    <File Ver="26e31dd8-85e1-4762-8101-dc6715de7d01" Name="fdsfs.wmv" />
    <File Ver="892fc9cc-b4fd-42b1-a235-09bfa8f87e21" Name="test/4454.mp3" />
  </Files>
  <Update>
    <Soft Name="BlogWriter">
      <Verson>b9c69d80-203d-4280-8ffe-1e4ae6ca621c</Verson>
    </Soft>
  </Update>
</AutoUpdater>

      在每一个xml配置文件中都有文件头

三、程序代码生成介绍

      程序如下:输入地址,点击生成,就可以得到生成的xml文件内容。


这里写图片描述

      生成xml文件:

       #region 创建xml文件-固定的格式-王雷-2017年4月24日16:24:35
        /// <summary>
        /// 创建xml文件-固定的格式-王雷-2017年4月24日16:24:35
        /// </summary>
        /// <param name="url">webservice的地址</param>
        void CreateXml(string url)
        {
            //创建文档对象
            XmlDocument doc = new XmlDocument();
            //创建根节点
            XmlElement root = doc.CreateElement("AutoUpdater");
            //头声明
            XmlDeclaration xmldecl = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(xmldecl);
            DirectoryInfo dicInfo = new DirectoryInfo(currentDirectory);

            //Updater
            XmlElement body1 = doc.CreateElement("Updater");
            Tool.AddChildNode(body1, doc, "Url", url);
            root.AppendChild(body1);

            //Application
            XmlElement body2 = doc.CreateElement("Application");
            Tool.AddEleAttr(body2, doc, "applicationId", "ItemSoft");
            Tool.AddChildNode(body2, doc, "EntryPoint", "ItemSoft");
            Tool.AddChildNode(body2, doc, "Location", ".");
            root.AppendChild(body2);

            //Files
            XmlElement body3 = doc.CreateElement("Files");
            //调用递归方法组装xml文件
            PopuAllDirectory(doc, body3, dicInfo);
            root.AppendChild(body3);

            //Update
            XmlElement body4 = doc.CreateElement("Update");
            root.AppendChild(body4);
            XmlElement body5 = doc.CreateElement("Soft");
            Tool.AddEleAttr(body5, doc, "Name", "BlogWriter");
            Tool.AddChildNode(body5, doc, "Verson", Guid.NewGuid().ToString());
            body4.AppendChild(body5);

            //追加节点
            doc.AppendChild(root);
            //保存文档
            doc.Save(serverXmlName);
        } 
        #endregion

      在其中用到了两个方法:

      向XML元素添加子节点:

         #region XML元素添加子节点-王雷-2017年4月24日16:26:37  
        /// <summary>  
        /// XML元素添加子节点-王雷-2017年4月24日16:26:37  
        /// </summary>  
        public static void AddChildNode(this XmlElement src, XmlDocument doc, string name, string innerText)
        {
            XmlElement elem = doc.CreateElement(name);
            elem.InnerText = innerText;
            src.AppendChild(elem);
        } 
        #endregion

      向XML元素添加属性:

         #region XML元素添加属性-王雷-2017年4月24日16:27:46 
        /// <summary>  
        /// XML元素添加属性-王雷-2017年4月24日16:27:46 
        /// </summary>  
        public static void AddEleAttr(this XmlElement src, XmlDocument doc, string name, string value)
        {
            XmlAttribute attr = doc.CreateAttribute(name);
            attr.Value = value;
            src.Attributes.Append(attr);
        } 
        #endregion

      递归的方式组装xml文件方法:

         //递归组装xml文件方法
        private void PopuAllDirectory(XmlDocument doc, XmlElement root, DirectoryInfo dicInfo)
        {
            foreach (FileInfo f in dicInfo.GetFiles())
            {
                //排除当前目录中生成xml文件的工具文件
                if (f.Name != "CreateXmlTools.exe" && f.Name != "AutoupdateService.xml" && f.Name != "AutoUpdate.exe" && f.Name.LastIndexOf(".pdb") == -1 && f.Name != "UpdateList.xml" && f.Name.LastIndexOf(".vshost.exe") == -1 && f.Name.LastIndexOf(".vshost.exe.manifest") == -1)
                {
                    string path = dicInfo.FullName.Replace(currentDirectory, "").Replace("\\", "/");
                    //path = dicInfo.FullName.Replace(currentDirectory, "").Replace(@"\", "/");
                    string folderPath=string.Empty;
                    if (path != string.Empty)
                    {
                        folderPath = path.TrimStart('/') + "/";
                    }
                    XmlElement child = doc.CreateElement("File");
                    child.SetAttribute("Ver", Guid.NewGuid().ToString());
                    child.SetAttribute("Name", folderPath + f.Name);
                    root.AppendChild(child);
                }
            }

            foreach (DirectoryInfo di in dicInfo.GetDirectories())
                PopuAllDirectory(doc, root, di);
        }

      读取xml文件:

 private void ReadXml()
        {
            string path="UpdateList.xml";
            rtbXml.ReadOnly = true;
            if (File.Exists(path))
            {
                rtbXml.Text = File.ReadAllText(path);
            }
        }

四、小结

      通过这次的接触,自己动手拼xml文件,把节点和属性一个一个的拼接上,然后就可以得到自己需要的配置文件,不用手写了,这样可以保证效率提高,出错率降低了。很不错的方式。加油!

      写博客不容易,官人,打个赏呗~~

这里写图片描述

福利:软件更新生成配置文件源码

评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你个佬六

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值