winform在线更新系统

View Code
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Net;
using System.Collections;

namespace 程序自动升级
{
    class Program
    {
        static void Main(string[] args)
        {
            string RemotePath = @"http://192.168.1.200/AutoUpdate.xml"; //远程xml文件地址
            string LocalVertion = GetLocalTime(Path.Combine(System.Environment.
                              CurrentDirectory, @"AutoUpdate.xml"));     //本地版本号
            string RemoteVersion = GetRemoteTime(RemotePath);            //远程版本号
            bool   IsUpdate=false;                                       //是否需要更新
            Console.WriteLine("    本地版本:" + LocalVertion);
            Console.WriteLine("服务器端版本:" + RemoteVersion);
            if (LocalVertion == RemotePath)
            {
                Console.WriteLine("不需要更新!");
            }
            else
            {
                
            }
             
            Console.WriteLine();
            Console.Read();
         
           
        }


        private void DeleteFiles()
        {
            DirectoryInfo directInfo = new DirectoryInfo(@"d:\Debug\");
            if (directInfo.Exists == true)
            {
                directInfo
            }
        }

        /// <summary>
        /// 读取xml信息
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        private static string GetLocalTime(string path)
        {
            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 == "Version")
                {
                    //获取升级文档的最后一次更新日期
                    LastUpdateTime = xml.GetAttribute("key");
                    break;
                }
            }
            xml.Close();
            myFile.Close();
            return LastUpdateTime;
        }
        /// <summary>
        /// 读取远程xml信息
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        private static string  GetRemoteTime(string  URL)
        { 
            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("/UpDateInfo/Time").Attributes[0].Value.ToString();
            return result;
        }

    }
}

 

posted @ 2012-11-27 17:21  尼姑哪里跑  阅读(182)  评论(0编辑  收藏  举报