您的位置:首页 > 其它

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

2017-04-24 16:35 465 查看

一、前言

在上一篇博客中,小编向大家介绍了【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文件,把节点和属性一个一个的拼接上,然后就可以得到自己需要的配置文件,不用手写了,这样可以保证效率提高,出错率降低了。很不错的方式。加油!

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



福利:软件更新生成配置文件源码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐