生成当前目录文件的xml描述

需求场景:例如需要在当前目录下把相关文件组织成xml文件去描述。通常在组织项目中的升级文件时候可能会用到。

代码示例

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace GenerateUpgradeXml
{
    class Program
    {
        //服务端升级xml文件名称
        static readonly string serverXmlName = "AutoupdateService.xml";
        //程序本身名称
        static readonly string selfName = "GenerateUpgradeXml.exe";
        //更新文件URL前缀
        static readonly string host = "http://localhost";
        //获取当前目录
        static readonly string currentDirectory = System.Environment.CurrentDirectory;

        static void Main(string[] args)
        {
            CreateXml();

            Console.WriteLine("生成完毕!");
            Console.ReadKey();
        }

        /// <summary>
        /// 创建xml文件
        /// </summary>
        private static void CreateXml()
        {
            //创建xml文档对象
            XmlDocument doc = new XmlDocument();

            //创建根节点
            XmlElement root = doc.CreateElement("updateFiles");

            //头声明
            XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(declaration);

            //获取当前目录对象
            DirectoryInfo directoryInfo = new DirectoryInfo(currentDirectory);
            //调用递归方法组装xml文件
            RecursiveDirectory(doc, root, directoryInfo);

            //追加根节点
            doc.AppendChild(root);

            //保存文档
            doc.Save(serverXmlName);
        }

        /// <summary>
        /// 递归目录下所有文件
        /// </summary>
        public static void RecursiveDirectory(XmlDocument doc,XmlElement root,DirectoryInfo directoryInfo)
        {
            //遍历目录下的所有文件
            foreach (FileInfo file in directoryInfo.GetFiles())
            {
                //过滤生成后的升级xml文件和程序本身文件
                if (file.Name== serverXmlName || file.Name== selfName)
                {
                    continue;
                }

                //路径转换
                string path = directoryInfo.FullName.Replace(currentDirectory,"").Replace("\\","/");

                //创建节点
                XmlElement child = doc.CreateElement("file");
                child.SetAttribute("path", file.Name);
                child.SetAttribute("url", host + path + "/" + file.Name);
                
                //当前目录下的文件夹及附属文件
                string fileName = file.Name;
                if (!string.IsNullOrEmpty(path))
                {
                    fileName = currentDirectory + path.Replace("/", "\\") + "\\" + file.Name;
                }

                child.SetAttribute("lastver", FileVersionInfo.GetVersionInfo(fileName).FileVersion);
                child.SetAttribute("size", file.Length.ToString());
                child.SetAttribute("needRestart", "false");

                root.AppendChild(child);
            }

            //遍历子目录并递归
            foreach (DirectoryInfo di in directoryInfo.GetDirectories())
            {
                RecursiveDirectory(doc, root, di);
            }
        }
    }
}

效果图: 

 文件详情

 

posted @ 2019-01-22 18:41  江宁织造  阅读(750)  评论(0编辑  收藏  举报