一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

ClickOnce DIY全自動更新下載升級的自我實現

SmartClient概念近來比較熱,但在微軟提出這個名詞以前已經有大量的軟件在這么做了,一方面是簡化客戶端的部署,一方面是提供自動升級的功能;對于傳統的WinForm應用來講,確實是可以降低維護成本的一個不錯的解決方案;
 微軟在推出SmartClient概念時,推出了相關的updater的Application Block,做的也蠻不錯,但作者前段還是根據軟件特性自己寫了一個很簡單的實現,大家也大概能了解一下原理:
筆者的簡化版自動升級管理器只需要四步走:
1.一個負責查找和下載新版本的本地類
2.本地配置文件中(或在代碼中硬編碼?不太好吧),指向更新服務器的URL
3.服務器上一個標識版本號和新文件URL的配置文件
4.調用示例
1.版本管理類
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.NET;
using System.IO;
using System.Windows.Forms;
namespace Survey
{
    class VersionAgent
    {
        public static bool CheckNETwork()
        {
            HttpWebRequest request;
            try
            {
                request = (HttpWebRequest)WebRequest.Create(Pub.GetSetting("UpdateUrl") );//從本地配置文件獲取的網絡中配置文件的URL
                request.Proxy = WebProxy.GetDefaultProxy();
                request.GetResponse();//如果可以獲得響應,說明網絡沒問題
            }
            catch (Exception e)
            {
                Pub.logError(e);
                return false;
            }
            return true;
        }

        public static bool CheckUpdate()
        {
            XmlDocument doc = loadXMLDocument(Pub.GetSetting("UpdateUrl"));
            Sys.UpdateUrl = GetValue(doc, "DownloadURL").Trim();//將來會用這個URL自動下載 
            Sys.UpdatePage = GetValue(doc, "DownloadPage").Trim();//如自動下載失敗,會提供到這個頁面手工下載
            string warningRate = GetValue(doc, "WarningRate").Trim();
            float.TryParse(warningRate,out Sys.WarningRate);
            string NETVersion = GetValue(doc, "Version").Trim();
Version LocalVersion=System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
            return new Version(NETVersion).CompareTo(new Version(LocalVersion))>0;//大于0說明有新版本發布
        }//這個方法是載入網絡配置文件,讀取一些不想放在本地的配置參數,以及比較本地和網絡版本號
        public static bool GoUpdate()
        {
          return DownLoadFile(Sys.UpdateFile,Sys.UpdateUrl);

        }
        public static string GetValue(XmlDocument doc, string Key)
        {
            string Value;
            try
            {
                XmlElement elem = (XmlElement)doc.SelectSingleNode(@"/config/app/" + Key);//讀取配置文件可自行定義
                Value = elem == null ? "" : elem.GetAttribute("value"); 
            }
            catch
            {
                Value = "";
            }
            return Value;
        }
        public static XmlDocument loadXMLDocument(string FileNameOrUrl)
        {
            XmlDocument doc = null;
            try
            {
                doc = new XmlDocument();
                doc.Load( FileNameOrUrl);
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message);
                Pub.logError(e);
                doc = null;
            }
            return doc;
        }

        public static bool DownLoadFile(string FileName, string Url)
        {
            bool Value = false;
            WebResponse response = null;
            Stream stream = null;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                response = request.GetResponse();
                stream = response.GetResponseStream();
                if (!response.ContentType.ToLower().StartsWith("text/"))
                {
                    Value = SaveBinaryFile(response, FileName);
                }
            }
            catch (Exception e)
            {
               // System.Windows.Forms.MessageBox.Show(e.Message);
                Pub.logError(e);
            }
            return Value;
        }

        private static bool SaveBinaryFile(WebResponse response, string FileName)
        {
            bool Value = true;
            byte[] buffer = new byte[1024];
            try
            {
                if (File.Exists(FileName))
                    File.Delete(FileName);
                Stream outStream = System.IO.File.Create(FileName);
                Stream inStream = response.GetResponseStream();
                int l;
                do
                {
                    l = inStream.Read(buffer, 0, buffer.Length);
                    if (l > 0)
                        outStream.Write(buffer, 0, l);
                }
                while (l > 0);
                outStream.Close();
                inStream.Close();
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message);
                Pub.logError(e);
                Value = false;
            }
            return Value;
        }
    }
}
2.本地配置文件可能如:
<configuration>
  <appSettings>
    <add key="UpdateUrl" value="http://www.abc.com/download/release.xml" />
  </appSettings>
</configuration>
3.網絡配置文件可能如:
<config>
  <app>
    <Version value="1.1.9.2" />
    <ReleaseDate value="2006-12-12" />
    <DownloadPage value="http://www.abc.com/download/index.htm" />
    <DownloadURL value="http://www.abc.com/download/update.exe" />
   <WarningRate value="0.3" />
  </app>
</config>
4.調用示例
在認為合適的時機(比如說應用程序啟動時),啟動一個后臺線程去工作:
            Thread thread = new Thread(new ThreadStart(threadMethodUpdate));
            thread.Start();

        private void threadMethodUpdate()
        {

            if (VersionAgent.CheckNETwork())//網絡狀況正常
            {
                if (VersionAgent.CheckUpdate())//檢查更新并獲取網絡參數
                {
                    if (VersionAgent.GoUpdate())//獲取新版本(由于我的軟件很小,所以在不提示用戶的情況就進行了新版下載,如認為不妥,可通過MessageBox提示一下)
                    {
                        MessageBox.Show("檢測到產品的更新版本,即將開始自動更新!", "版本升級", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        System.Diagnostics.Process.Start(Sys.UpdateFile);
                        System.Environment.Exit(0);
                    }
                    else
                    {
                        MessageBox.Show("系統檢測到更新版本,但自動下載失敗,點擊確定進行手動下載", "版本升級", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        System.Diagnostics.Process.Start(Sys.UpdatePage);
                        System.Environment.Exit(0);
                    }
                }
            }
            else//也可以什么也不提示
                MessageBox.Show("無法連接到服務器進行自動升級!/n請檢查網絡連接 " + Pub.GetSetting("UpdateUrl"), "網絡異常", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

AspNet技術ClickOnce DIY全自動更新下載升級的自我實現,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 夜夜揉揉日日人人视频 | 三级韩国一区久久二区综合 | 香蕉97碰碰视频免费 | 欧美国产91 | 伊人色网站 | 69女poren60 | 在线xx视频 | 久久99草 | 国产精品100页 | 2021最新国产成人精品免费 | 一区二区三区日韩精品 | 黑猫福利精品第一视频 | 成人激情四射网 | 色女人在线 | 国产在线播 | 亚洲一区二区中文字幕 | 婷婷热| 狠狠五月深爱婷婷网免费 | 久久99久久99| 91久操| 天天爽天天操 | 日本护士xxxxx18.19 | 国产专区青青草原亚洲 | 日本99热 | 日韩欧美一二区 | 91这里只有精品 | 中文字幕日韩精品中文区 | 成人国产一区二区 | 免费国产怡红院在线观看 | 免费在线小视频 | 特级做a爰片毛片免费看一区 | 91精品福利在线 | 91色视| 四虎在线最新永久免费 | 91精品免费高清在线 | 麻豆综合网 | 女神级极品嫩模露脸啪啪自拍 | 伊人久久综在合线亚洲91 | 日本美女视频韩国视频网站免费 | 一本久道久久综合 | 中文字幕一区二区三区 精品 |