zoukankan      html  css  js  c++  java
  • Winform(通过IIS服务器实现更新功能)

    代码如下
    /*Made by Anby
     * 2012-11-28
     * 实现软件从iis服务器更新版本
     * 自动检测当前版本号,从服务器下载
     * 
     * 
     * //IIS服务器 增加MIME类型
     * .*
     * application/octet-stream
     * 
     * 
     * xml文件格式如下:
     <?xml version="1.0" encoding="utf-8" ?>
    <UpDateInfo>
      <Version key="20121125" />
      <FileInfo>
        <FileCount key="6" />
        <File1 key="desktop.db" />
        <File2 key="SQLite Database Browser.exe" />
        <File3 key="System.Data.SQLite.dll" />
        <File4 key="桌面提醒插件.exe" />
        <File5 key="桌面提醒插件.vshost.exe" />
        <File6 key="桌面提醒插件.vshost.exe.manifest" />
      </FileInfo>
    </UpDateInfo>
      
     * ini文件格式
    [Server]
    URL=http://192.168.1.200/    //在该目录下存放AutoUpdate.xml文件
    [Local]
    ProcessName=桌面提醒插件
    SavePath=D:\Debug\
     * 版权所有
     */
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.Xml;
    using System.Net;
    using System.Collections;
    using System.DirectoryServices;
    using System.Diagnostics;
    using DotNet.Utilities;
    
    namespace 程序自动升级
    {
        class Program
        {
            static void Main(string[] args)
            {
                string Server = GetServerUri();                    //远程IIs服务器
                string RemotePath = Server+"AutoUpdate.xml";   //远程xml文件地址
                string LocalVertion  = GetLocalVersion(Path.Combine(System.Environment.
                                       CurrentDirectory, @"AutoUpdate.xml"),"Version","key");     //本地版本号
                string RemoteVersion = GetRemoteVersion(RemotePath, "/UpDateInfo/Version");       //远程版本号
                Console.WriteLine("    本地版本:" + LocalVertion);
                Console.WriteLine("服务器端版本:" + RemoteVersion);
                if (LocalVertion == RemoteVersion)
                {
                    Console.WriteLine("已经是最新版本,不需要更新!");
                }
                else
                {
                    string ProcName = GetProcessName() ;
                    string SavePath = GetSavePath();
                    //关闭要升级的程序
                    ProcessKill(ProcName);
                    Console.WriteLine(ProcName+"进程结束成功!");
                    //删除对应的文件
                    DeleteLocalFiles(SavePath);
                    Console.WriteLine(SavePath+"文件已删除成功!");
                    //实现文件的下载
                    int Filecount = int.Parse(GetRemoteVersion(RemotePath, "/UpDateInfo/FileInfo/FileCount"));
                    for (int i = 1; i <= Filecount; i++)
                    {
                        string node = GetRemoteVersion(RemotePath, "/UpDateInfo/FileInfo/File" + i);
                        if (node == "AutoUpdate.xml") //如果文件时更新使用的xml则将本地的xml文件覆盖
                            DownLoadFile(Server + node,node);
                        //下载更新文件
                        DownLoadFile(Server + node, SavePath + node);
                        Console.WriteLine(node+"已经下载完成!"); 
                    }
                }
                Console.WriteLine("更新成功!");
                if (OpenFile())
                {
                    Console.WriteLine("文件打开成功!");
                }
                else
                {
                    Console.WriteLine("文件打开失败!");
                }
                Console.Read();
            }
            /// <summary>
            /// 下载指定uri中的文件
            /// </summary>
            /// <param name="path">要下载文件的uri路径</param>
            /// <param name="fileName">要保存的文件名及路径,默认文件存放在Application.Startup</param>
            private static void DownLoadFile(string uri,string fileName)
            { 
                WebClient WebCnt = new WebClient();
                WebCnt.DownloadFile(uri, fileName);
            }
            /// <summary>
            /// 删除本地d:\Debug\中的文件
            /// </summary>
            private static void DeleteLocalFiles(string Path)
            {
                DirectoryInfo Dir = new DirectoryInfo(Path);
                FileInfo[] File = Dir.GetFiles();
                //删除本地文件d:\Debug\
                foreach (FileInfo item in File)
                {
                    if (item.Exists)
                    {
                        item.Delete();
                    }
                }
            }
            /// <summary>
            /// 读取本地xml的版本信息
            /// </summary>
            /// <param name="path">uri路径:</param>
            /// <param name="Name">如:"Version"</param>
            /// <param name="Attribute">如:"key"</param>
            /// <returns>本地节点的值:version/ key下的值</returns>
            private static string  GetLocalVersion(string path,string Name,string Attribute)
            {
                string LastUpdateTime = "";
                string AutoUpdaterFileName = path;
                if (!File.Exists(AutoUpdaterFileName))
                    return LastUpdateTime;//打开xml文件
                FileStream myFile = new FileStream(AutoUpdaterFileName, FileMode.Open);
                //xml文件阅读器
                XmlTextReader xml = new XmlTextReader(myFile);
                while (xml.Read())
                {
                    if (xml.Name == Name)
                    {
                        //获取升级文档的最后一次更新日期
                        LastUpdateTime = xml.GetAttribute(Attribute);
                        break;
                    }
                }
                xml.Close();
                myFile.Close();
                return LastUpdateTime;
            }
            /// <summary>
            /// 读取远程xml中版本信息
            /// </summary>
            /// <param name="path">远程AutoUpdate.xml的路径如:http://192.168.1.1/AutoUpdate.xml</param>
            /// <param name="node">键名的路径:如"/UpDateInfo/Version"</param>
            /// <returns>返回远程xml中/UpDateInfo/Version"的属性值,如:Version键的属性key</returns>
            private static string  GetRemoteVersion(string  URL,string node)
            { 
                string url=URL;
                //创建一个WebRequest对象对URl请求
                WebRequest WebReq = System.Net.WebRequest.Create(url);
                //创建一个WebResponse对象,用来接收URl资源的返回
                WebResponse myResponse = WebReq.GetResponse();
                //创建一个文件流,存储返回的数据流
                Stream fileStream = myResponse.GetResponseStream();
                //创建一个内存中的xml文件
                XmlDocument xml = new XmlDocument();
                //用虚拟文件来装载文件流
                xml.Load(fileStream);
                //获取节点值
                string result = xml.SelectSingleNode(node).Attributes[0].Value.ToString();
                return result;
            }
    
    
            /// <summary>
            /// 结束进程
            /// </summary>
            /// <param name="processname">进程名</param>
            /// <returns>成功返回true,否则返回true</returns>
            private static bool ProcessKill(string processname)
            { 
                Process[] ProList = Process.GetProcesses();
                foreach (Process item in ProList)
                {
                    if (item.ProcessName == processname)
                    {
                        item.Kill();
                        return true;
                    }
                    
                }
                return false;
            }
    
            private static string GetServerUri()
            {
                INIFile inif = new INIFile(Environment.CurrentDirectory + "\\Config.ini");
                FileInfo fileinf=new FileInfo(Environment.CurrentDirectory + "\\Config.ini");
    
                if (fileinf.Exists)
                { 
                    inif.IniWriteValue("Server","URL",@"http://192.168.1.200/");
                }
                string result= inif.IniReadValue("Server","URL");
                return result;
            }
            private static string GetSavePath()
            {
                INIFile inif = new INIFile(Environment.CurrentDirectory + "\\Config.ini");
                 FileInfo fileinf=new FileInfo(Environment.CurrentDirectory + "\\Config.ini");
    
                if (fileinf.Exists)
                { 
                    inif.IniWriteValue("Local","SavePath",@"D:\Debug\");
                }
                string result = inif.IniReadValue("Local", "SavePath");
                return result;
            }
            private static string GetProcessName()
            {
                INIFile inif = new INIFile(Environment.CurrentDirectory + "\\Config.ini");
                 FileInfo fileinf=new FileInfo(Environment.CurrentDirectory + "\\Config.ini");
    
                if (fileinf.Exists)
                {
                    inif.IniWriteValue("Local", "ProcessName", "桌面提醒插件");
                }
                string result = inif.IniReadValue("Local", "ProcessName");
                return result;
            }
            private static bool OpenFile()
            {
                Process ps = new Process();
                ps.StartInfo.FileName = GetSavePath() + GetProcessName() + ".exe";
                if (ps.Start())
                { 
                    return true;
                }
                else 
                { 
                    return false;
                }
            }
    
        }
    }
  • 相关阅读:
    Kafka文件存储机制那些事(转发)
    Kafka文件存储机制那些事(转发)
    消息队列设计精要(转发)
    RocketMQ原理解析-Broker(转发)
    Apache Kafka:下一代分布式消息系统(转发)
    新浪技术分享:我们如何扛下32亿条实时日志的分析处理(转发)
    消息队列技术介绍(转发)
    confluent kafka for .net
    kafka参考资料
    kafka架构(转发)
  • 原文地址:https://www.cnblogs.com/anbylau2130/p/2794910.html
Copyright © 2011-2022 走看看