增加了程序自動更新版本的功能,實現方式如下:
后台數據庫中用一張表來保存程序的版本信息,該表的字段很簡單,如下:
CREATE TABLE [dbo].[sys_AutoUpdate]( [UID] [int] IDENTITY(1,1) NOT NULL, [SystemName] [varchar](50) NULL, [SystemVersion] [varchar](10) NULL, [Remark] [text] NULL, [UpdateDate] [datetime] NULL, [UpdatePath] [varchar](500) NULL)
在之前的項目里面增加了一個新的項目,主要用來實現更新,通過"參數設置"界面維護必須要的信息,如更新的文件列表,
更新的路徑,需要更新的程序名稱,版本以及描述信息等等,如下圖:
將需要更新的文件放置更新的目錄中,這樣客戶端才可以得到最新的文件,程序里面會記錄着每次的版本號,如下代碼:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using Allen.Model; 6 7 namespace Allen.Tools.Common 8 { 9 /// <summary> 10 /// 這個類很重要,主要用來保存 一些臨時信息,可以在整個項目中使用 11 /// </summary> 12 public sealed class AllenSingleton 13 { 14 private static volatile AllenSingleton instance; 15 private static object syncRoot = new Object(); 16 17 private AllenSingleton() { } 18 public static AllenSingleton Instance 19 { 20 get 21 { 22 if (instance == null) 23 { 24 lock (syncRoot) 25 { 26 if (instance == null) 27 instance = new AllenSingleton(); 28 } 29 } 30 return instance; 31 } 32 } 33 34 public string strCon = Allen.Model.ConnectionModel.ConnectionString1; 35 public string UserID; 36 public string Password; 37 public string UserDep; 38 public string ServerID; 39 public string Lang; 40 public FrmMain m_FrmMain; 41 public string Company; 42 public string AppConfigFile; 43 public Dictionary<string, string> DicLang; 44 public string Role; 45 public string RoleName; 46 public string currentlyVersion = "1.001"; 47 public string SystemName = "Allen.Tools"; 48 49 50 public string AllowCreate; 51 public string AllowDelete; 52 public string AllowEdit; 53 public string AllowPrint; 54 55 56 //public static class GlobalData 57 //{ 58 // public static Dictionary<string, Action> dict = new Dictionary<string, Action>(); 59 //} 60 61 62 } 63 }
客戶端在登錄的時候進行版本檢查:
1 double NewVer = Convert.ToDouble(new BLL.sys_AutoUpdateManager().GetSystemVersionInfo(allensingleton.SystemName).Rows[0]["SystemVersion"].ToString()); 2 double CurrVer = Convert.ToDouble(allensingleton.currentlyVersion); 3 if (NewVer > CurrVer) 4 { 5 6 DialogResult dr = MessageBox.Show("發現新的版本,是否要更新該軟件?", "系統提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); 7 if (dr == DialogResult.OK) 8 { 9 Process.Start(Application.StartupPath+"\\AutoUpdate.exe"); 10 Thread.Sleep(500); 11 this.Dispose(); 12 this.Close(); 13 } 14 }
如果版本低於服務器上面的版本,那么則啟動更新程序:
待更新結束之后再重新打開最新版本的程序,主程序和更新程序放在同一目錄里面。