C#软件版本管理--XML用法

  1. 开发背景

顺接上期需求,客户要求管理工具能够自动更新,并且强调了一点:一定要悄悄的运行,发现新版本,才提醒“是否进行更新?”。

因此,我们做一个软件版本列表,记录当前使用的软件文件版本。

  1. 实现功能
  1. 获取组件(*.dll)的版本号、文件的MD5,用于和服务器文件校验。
  2. XML操作方法,读取、写入属性值。
  3. 在一个XML文件中,分别记录组件(*.dll)版本号;文件(*.exe/*.config/*.xml等)的MD5值。
  1. 源码解析

/// <summary>
        /// 获取源文件版本号
        /// </summary>
        /// <param name="a_strSourceFile"></param>
        /// <returns></returns>
        private static string GetFileVer(string a_strSourceFile)
        {
            string l_strRet = "";
            try
            {
                FileVersionInfo ff = FileVersionInfo.GetVersionInfo(a_strSourceFile);
                l_strRet = ff.FileVersion;
            }
            finally
            {
                GC.Collect();
            }
            return l_strRet;
        }
        
        /// <summary>
        /// 获取源文件MD5值
        /// </summary>
        /// <param name="a_strSourceFile"></param>
        /// <returns></returns>
        private static string GetFileMD5(string a_strSourceFile)
        {
            System.Text.StringBuilder l_bfRet = new System.Text.StringBuilder();
            System.IO.FileStream fs = new System.IO.FileStream(a_strSourceFile, System.IO.FileMode.Open,
                                                               FileAccess.Read, FileShare.ReadWrite);
            try
            {
                System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                md5.ComputeHash(fs);
                foreach (byte b in md5.Hash)
                {
                    l_bfRet.Append(string.Format("{0:X1}", b));
                }
            }
            finally
            {
                fs.Close();
            }
            return l_bfRet.ToString();
        }
        
        #region 获取属性
        /// <summary>
        /// 取文件值(非*.dll文件)
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string GetValueForFile(string key)
        {
            if (!File.Exists(this.FilePath))
            {
                File.WriteAllText(this.FilePath,UpdateXmlExtension.InitContent);
            }
            
            string result = "";
            try
            {
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(this.FilePath);
                string xpath = "//file [@file_name=\"" + key + "\"]/@file_md5";
                XmlNode xmlNode = xmlDocument.SelectSingleNode(xpath);
                result = xmlNode.Value;
            }
            finally
            {
            }
            return result;
        }
        
        /// <summary>
        /// 取组件值(*.dll)
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string GetValueForFun(string key)
        {
            if (!File.Exists(this.FilePath))
            {
                File.WriteAllText(this.FilePath,UpdateXmlExtension.InitContent);
            }
            
            string result = "";
            try
            {
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(this.FilePath);
                string xpath = "//fun [@fun_no=\"" + key + "\"]/@assembly_ver";
                XmlNode xmlNode = xmlDocument.SelectSingleNode(xpath);
                result = xmlNode.Value;
            }
            finally
            {
            }
            return result;
        }
        #endregion
        
        #region 设置属性
        /// <summary>
        /// 设置值(非*.dll文件)
        /// </summary>
        /// <param name="a_strFunNo"></param>
        /// <param name="a_strFileMD5"></param>
        public void SetValueForFile(string a_strFunNo, string a_strFileMD5)
        {
            XmlDocument xmlDocument = new XmlDocument();
            XmlElement xmlElement;
            if (File.Exists(this.FilePath))//xml文件不存在
            {
                xmlDocument.Load(this.FilePath);//加载xml
                XmlElement documentElement = xmlDocument.DocumentElement;
                xmlElement = (documentElement.FirstChild as XmlElement);//定位第一个子节点
                if(xmlElement == null)//没有该子节点,返回null
                {
                    xmlElement = xmlDocument.CreateElement("FileList");//则创建FileList子节点
                    documentElement.AppendChild(xmlElement);
                }
            }
            else
            {
                xmlDocument.AppendChild(xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", ""));
                xmlDocument.AppendChild(xmlDocument.CreateElement("configuration"));
                XmlElement documentElement = xmlDocument.DocumentElement;
                xmlElement = xmlDocument.CreateElement("FileList");
                documentElement.AppendChild(xmlElement);
            }
            
            try
            {
                string l_strQuery = string.Format("//file [@file_name=\"{0}\"]", a_strFunNo);//例如://file [@file_name="K1"]"
                XmlNode xmlNode = xmlDocument.SelectSingleNode(l_strQuery);//定位到该叶节点
                if (xmlNode == null) {
                    XmlElement xmlElement2 = xmlDocument.CreateElement("file");//元素名
                    xmlElement2.SetAttribute("file_name", a_strFunNo);//设置属性
                    xmlElement2.SetAttribute("file_md5", a_strFileMD5);
                    xmlElement.AppendChild(xmlElement2);
                } else {
                    XmlElement xmlElement2 = xmlNode as XmlElement;
                    xmlElement2.SetAttribute("file_name", a_strFunNo);
                    xmlElement2.SetAttribute("file_md5", a_strFileMD5);
                }
            }catch{
                File.Delete(this.FilePath);
            }
            finally {
                xmlDocument.Save(this.FilePath);
            }
        }
        
        /// <summary>
        /// 设置值(*.dll文件)
        /// </summary>
        /// <param name="a_strFunNo"></param>
        /// <param name="a_strAssemblyVer"></param>
        public void SetValueForFun(string a_strFunNo, string a_strAssemblyVer)
        {
            XmlDocument xmlDocument = new XmlDocument();
            XmlElement xmlElement;
            if (File.Exists(this.FilePath))
            {
                xmlDocument.Load(this.FilePath);
                XmlElement documentElement = xmlDocument.DocumentElement;
                xmlElement = (documentElement.SelectSingleNode("Presentation") as XmlElement);//定位名为"Presentation"子节点
                if(xmlElement == null)
                {
                    xmlElement = xmlDocument.CreateElement("Presentation");
                    documentElement.AppendChild(xmlElement);
                }
            }
            else
            {
                xmlDocument.AppendChild(xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", ""));
                xmlDocument.AppendChild(xmlDocument.CreateElement("configuration"));
                XmlElement documentElement = xmlDocument.DocumentElement;
                xmlElement = xmlDocument.CreateElement("Presentation");
                documentElement.AppendChild(xmlElement);
            }
            
            try
            {
                string l_strQuery = string.Format("//fun [@fun_no=\"{0}\"]", a_strFunNo);
                XmlNode xmlNode = xmlDocument.SelectSingleNode(l_strQuery);
                if (xmlNode == null) {
                    XmlElement xmlElement2 = xmlDocument.CreateElement("fun");
                    xmlElement2.SetAttribute("fun_no", a_strFunNo);
                    xmlElement2.SetAttribute("assembly_ver", a_strAssemblyVer);
                    xmlElement.AppendChild(xmlElement2);
                } else {
                    XmlElement xmlElement2 = xmlNode as XmlElement;
                    xmlElement2.SetAttribute("fun_no", a_strFunNo);
                    xmlElement2.SetAttribute("assembly_ver", a_strAssemblyVer);
                }
            }catch{
                File.Delete(this.FilePath);
            }
            finally {
                xmlDocument.Save(this.FilePath);
            }
        }
        #endregion

 

程序运行结果:

 

C#软件版本管理--XML用法_第1张图片

你可能感兴趣的:(底层库(工具,通用类),Xml文件读写,软件版本,MD5)