winform實現自動更新並動態調用form實現

來源:互聯網
上載者:User

    winform 應用系統的部署問題,一直是個令人苦惱的問題。在2.0下,我們可以通過clickonce方式部署,但是這裡要說的,是利用檔案的操作來對系統進行更新。

    為了方便使用者作業,把實際的系統打包成DLL存在,而設計一個自動更新的form來作為使用者啟動的入口。使用者啟動後,實現自動檢測服務端和用戶端系統檔案版本,並把所有所需檔案更新至最新版本。而後自動啟動系統入口介面。

    

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Reflection;
  9. using System.Threading;
  10. using System.Net;
  11. using System.IO;
  12. namespace autoUpdateAndCallFormDll
  13. {
  14.     /// <summary>
  15.     /// 1.啟動時,實現自動更新
  16.     /// 2.可把不同模組打包DLL,在運行時,利用反射機制調用DLL 中form(類)。
  17.     /// 
  18.     /// 為了準確顯示更新畫面,並在更新完畢後調用Dll,採用非同步方式。
  19.     /// </summary>
  20.     public partial class Form1 : Form
  21.     {
  22.         public Form1()
  23.         {
  24.             InitializeComponent();
  25.             backgroundWorker1.RunWorkerAsync(); //非同步更新作業
  26.         }
  27.         private void callMainForm()
  28.         {
  29.             //Dynamic Call Form using Assembly 
  30.             Assembly assembly = Assembly.LoadFrom(@"D:/TEST/SD.dll");
  31.             Form frm = assembly.CreateInstance("SD.frmmain") as Form;
  32.             frm.Show();
  33.             frm.Activate();
  34.             this.Visible = false;  
  35.           
  36.         }
  37.         private void updateDll()
  38.         {
  39.             string fileName = @"D:/TEST" + "//" + "dd.dll";   //本機路徑
  40.             string url = @"http://webtest/ServerDownload" + "/" + "a.dll";  //服務端路徑
  41.             try
  42.             {
  43.                 //採用http方式進行更新,避免網路,檔案夾許可權的限制。
  44.                 WebRequest myWebRequest = WebRequest.Create(url);
  45.                 WebResponse myWebResponse = myWebRequest.GetResponse();
  46.                 Stream receiveStream = myWebResponse.GetResponseStream();
  47.                 //先刪除曆史檔案
  48.                 File.Delete(fileName);
  49.                 FileStream fs = new FileStream(fileName, FileMode.CreateNew);
  50.                 try
  51.                 {
  52.                     List<byte> lstByte = new List<byte>();
  53.                     while (true)
  54.                     {
  55.                         //讀一個byte, 寫一個byte
  56.                         int i = receiveStream.ReadByte();
  57.                         if (i == -1)
  58.                         {
  59.                             break;
  60.                         }
  61.                         fs.WriteByte(Convert.ToByte(i));
  62.                     }
  63.                 }
  64.                 finally
  65.                 {
  66.                     if (fs != null)
  67.                         fs.Close();
  68.                     myWebResponse.Close();
  69.                 }
  70.                
  71.             }
  72.             catch (Exception ex)
  73.             {
  74.                 throw ex;
  75.             }
  76.             finally
  77.             {
  78.                 WebRequest.DefaultWebProxy = null;
  79.             }
  80.         }
  81.         private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
  82.         {
  83.             Thread.Sleep(3000);  //為了清晰的看到更新,可採用此方式
  84.             updateDll(); //非同步更新檔案
  85.         }
  86.         private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  87.         {
  88.             callMainForm();  //非同步作業完畢,調用此方法
  89.         }
  90.     }
  91. }

    範例是一個簡單的實現,實際作業中,可以對目錄操作,對每個檔案的版本(或者最後修改時間)進行比對,如果有新版本出現,則更新。

    要點:

    1.版本比對

    2.非同步作業(backgroundworker組件)

    3.http方式實現檔案下載(webRequest類)

    4.反射機制,實現form動態調用
    5.伺服器端檔案夾要在IIS中建立虛擬目錄,以實現http訪問

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.