zoukankan      html  css  js  c++  java
  • Winform(C#.NET)自动更新组件的使用及部分功能实现(一点改进功能)

    接前两篇继续:

    Winform(C#.NET)自动更新组件的使用及部分功能实现

    Winform(C#.NET)自动更新组件的使用及部分功能实现(续)

    借鉴文章:http://www.cnblogs.com/jeffersyuan/archive/2007/04/18/718108.html

    现象:

    由于dll文件比较多,再加之变更比较频繁,而每次点击createxmltools都会重新生成不同的version,

    而实际上文件并未发生变化,导致重复更新,浪费资源。

    解决方法:

    根据文件的信息生成一个哈希值,通过比较哈希值来判断文件是否进行了修改。

    主要使用类:MD5CryptoServiceProvider

    类的注释:使用加密服务提供程序 (CSP) 提供的实现,计算输入数据的 System.Security.Cryptography.MD5 哈希值。

            /// <summary>
            /// md5加密文件为hashcode,看文件是否真的有变化
            /// </summary>
            /// <param name="fileName">文件fullname</param>
            /// <returns>根据MD5CryptoServiceProvider加密后的一个值</returns>
            private string MD5(string fileName)
            {
                string temp = "";
                FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
                byte[] md5byte = md5.ComputeHash(fs);
    
                int i, j;
                foreach (byte b in md5byte)
                {
                    i = Convert.ToInt32(b);
                    j = i >> 4;
                    temp = temp + Convert.ToString(j, 16);
                    j = ((i << 4) & 0x00ff) >> 4;
                    temp = temp + Convert.ToString(j, 16);
                }
                return temp;
            }

    在createxmltools的项目修改

    foreach (FileInfo f in dicInfo.GetFiles())
    {
        //排除当前目录中生成xml文件的工具文件
        if (!getRidOfFiles.Contains(f.Name))
        {
            string path = dicInfo.FullName.Replace(currentDirectory, "").Replace("\", "/");
            string folderPath=string.Empty;
            if (path != string.Empty)
            {
                folderPath = path.TrimStart('/') + "/";
            }
            XmlElement child = doc.CreateElement("file");
            child.SetAttribute("path", folderPath + f.Name);
            child.SetAttribute("url", url + path + "/" + f.Name);
            child.SetAttribute("lastver", FileVersionInfo.GetVersionInfo(f.FullName).FileVersion);
            child.SetAttribute("size", f.Length.ToString());
            child.SetAttribute("needRestart", "false");
            child.SetAttribute("version", MD5(f.FullName));
            root.AppendChild(child);
        }
    }

    这样就能简单的判断文件是否被修改过了

    缺点:并不能真的判定文件没被修改,但是不会每个文件点击生成xml的时候生成不一样的哈希值。只有在dll重新编译的时候哈希值才可能会发生变化。

  • 相关阅读:
    Sqli-labs less 12
    Sqli-labs less 13
    Sqli-labs less 14
    Python3之collections模块
    Python3之 contextlib
    Python3之sqlalchemy
    Python3之RabbitMQ
    Python3之redis使用
    Python3之Memcache使用
    python自动化开发学习 进程, 线程, 协程
  • 原文地址:https://www.cnblogs.com/24la/p/autoupdater-component-add.html
Copyright © 2011-2022 走看看