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

编程入门 行业动态 更新时间:2024-10-22 16:27:23

【CS】客户端更新(二)——生成更新<a href=https://www.elefans.com/category/jswz/34/1770506.html style=配置文件程序介绍"/>

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

一、前言

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

二、配置文件结构分析

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

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

<?xml version="1.0" encoding="utf-8"?>
<AutoUpdater><Updater><Url>/</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);//UpdaterXmlElement body1 = doc.CreateElement("Updater");Tool.AddChildNode(body1, doc, "Url", url);root.AppendChild(body1);//ApplicationXmlElement body2 = doc.CreateElement("Application");Tool.AddEleAttr(body2, doc, "applicationId", "ItemSoft");Tool.AddChildNode(body2, doc, "EntryPoint", "ItemSoft");Tool.AddChildNode(body2, doc, "Location", ".");root.AppendChild(body2);//FilesXmlElement body3 = doc.CreateElement("Files");//调用递归方法组装xml文件PopuAllDirectory(doc, body3, dicInfo);root.AppendChild(body3);//UpdateXmlElement 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文件,把节点和属性一个一个的拼接上,然后就可以得到自己需要的配置文件,不用手写了,这样可以保证效率提高,出错率降低了。很不错的方式。加油!

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

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

更多推荐

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

本文发布于:2024-02-26 16:02:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1703073.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:配置文件   客户端   程序   CS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!