長期以來,廣大程序員為到底是使用Client/Server,還是使用Browser/Server結構爭論不休,在這些爭論當中,C/S結構的程序可維護性差,布置困難,升級不方便,維護成本高就是一個相當重要的因素。有很多企業用戶就是因為這個原因而放棄使用C/S。然而當一個應用必須要使用C /S結構才能很好的實現其功能的時候,我們該如何解決客戶端的部署與自動升級問題?部署很簡單,只要點擊安裝程序即可,難的在於每當有新版本發布時,能夠實現自動升級[3]。現在好了,我們的目標很簡單,我們希望開發一個與具體應用無關的能夠復用的自動升級系統。下面我為大家提供了一套可復用的用C#編寫的自動升級系統。
2 實現軟件的自動升級存在的困難
第一,為了查找遠程服務器上的更新,應用程序必須有查詢網絡的途徑,這需要網絡編程、簡單的應用程序與服務器通訊的協議。
第二是下載。下載看起來不需要考慮聯網的問題,但要考慮下載用戶請求的文件,以及在沒有用戶同意時下載大文件。友好的自動更新應用程序將使用剩余的帶寬下載更新。這聽起來簡單,但卻是一個技術難題,幸運的是已經有了解決方法。
第三個考慮因素是使用新版應用程序更換原應用程序的過程。這個問題比較有趣,因為它要求代碼運行時將自己從系統刪除,有多種辦法可以實現該功能[5],本文程序主要通過比較新舊版本的日期號來實現替換新版本應用程序的功能。
3 實現軟件自動在線升級的原理
寫兩個程序,一個是主程序;一個是升級程序;所有升級任務都由升級程序完成。
1.啟動升級程序,升級程序連接到網站,下載新的主程序(當然還包括支持的庫文件、XML配置文檔等)到臨時文件夾;
2.升級程序獲取服務器端XML配置文件中新版本程序的更新日期或版本號或文件大小;
3.升級程序獲取原有客戶端應用程序的最近一次更新日期或版本號或文件大小,兩者進行比較;如果發現升級程序的日期大於原有程序的最新日期,則提示用戶是否升級;或者是采用將現有版本與最新版本作比較,發現最新的則提示用戶是否升級;也有人用其它屬性如文件大小進行比較,發現升級程序的文件大小大於舊版本的程序的大小則提示用戶升級。本文主要采用比較新舊版本更新日期號來提示用戶升級。
4.如果用戶選擇升級,則獲取下載文件列表,開始進行批量下載文檔;
5.升級程序檢測舊的主程序是否活動,若活動則關閉舊的主程序;
6.刪除舊的主程序,拷貝臨時文件夾中的文件到相應的位置;
7.檢查主程序的狀態,若狀態為活動的,則啟動新的主程序;
8.關閉升級程序,升級完成[4]。
4 用C#實現在線升級的關鍵步驟
這里我主要使用日期信息來檢測是否需要下載升級版本。
4.1 准備一個XML配置文件
名稱為AutoUpdater.xml,作用是作為一個升級用的模板,顯示需要升級的信息。
名稱為AutoUpdater.xml,作用是作為一個升級用的模板,顯示需要升級的信息。
01 |
<?xml version="1.0"?> //xml版本號 |
02 |
<AutoUpdater> |
03 |
<URLAddres URL="http://192.168.198.113/vbroker/log/"/>//升級文件所在服務器端的網址 |
04 |
<UpdateInfo> |
05 |
<UpdateTime Date = "2005-02-02"/> //升級文件的更新日期 |
06 |
<Version Num = "1.0.0.1"/> //升級文件的版本號 |
07 |
</UpdateInfo> |
08 |
<UpdateFileList> //升級文件列表 |
09 |
<UpdateFile FileName = "aa.txt"/> //共有三個文件需升級 |
10 |
<UpdateFile FileName = "VB40.rar"/> |
11 |
<UpdateFile FileName = "VB4-1.CAB"/> |
12 |
</UpdateFileList> |
13 |
<RestartApp> |
14 |
<ReStart Allow = "Yes"/> //允許重新啟動應用程序 |
15 |
<AppName Name = "TIMS.exe"/> //啟動的應用程序名 |
16 |
</RestartApp> |
17 |
</AutoUpdater> |
18 |
19 |
//xml版本號 |
20 |
//升級文件所在服務器端的網址 |
21 |
//升級文件的更新日期 |
22 |
//升級文件的版本號 |
23 |
//升級文件列表 |
24 |
//共有三個文件需升級 |
25 |
//允許重新啟動應用程序 |
26 |
//啟動的應用程序名 |
從以上XML文檔中可以得知升級文檔所在服務器端的地址、升級文檔的更新日期、需要升級的文件列表,其中共有三個文件需升級:aa.txt、VB40.rar、VB4-1.CAB。以及是否允許重新啟動應用程序和重新啟動的應用程序名。
4.2 獲取客戶端應用程序及服務器端升級程序的最近一次更新日期
通過GetTheLastUpdateTime()函數來實現。
01 |
private string GetTheLastUpdateTime( string Dir) |
02 |
{ |
03 |
string LastUpdateTime = "" ; |
04 |
string AutoUpdaterFileName = Dir + @"\AutoUpdater.xml" ; |
05 |
if (!File.Exists(AutoUpdaterFileName)) |
06 |
return LastUpdateTime; |
07 |
//打開xml文件 |
08 |
FileStream myFile = new FileStream(AutoUpdaterFileName,FileMode.Open); |
09 |
//xml文件閱讀器 |
10 |
XmlTextReader xml = new XmlTextReader(myFile); |
11 |
while (xml.Read()) |
12 |
{ |
13 |
if (xml.Name == "UpdateTime" ) |
14 |
{ |
15 |
//獲取升級文檔的最后一次更新日期 |
16 |
LastUpdateTime = xml.GetAttribute( "Date" ); |
17 |
break ; |
18 |
} |
19 |
} |
20 |
xml.Close(); |
21 |
myFile.Close(); |
22 |
return LastUpdateTime; |
23 |
} |
通過XmlTextReader打開XML文檔,讀取更新時間從而獲取Date對應的值,即服務器端升級文件的最近一次更新時間。
函數調用實現:
//獲取客戶端指定路徑下的應用程序最近一次更新時間
string thePreUpdateDate = GetTheLastUpdateTime(Application.StartupPath);
Application.StartupPath指客戶端應用程序所在的路徑。
//獲得從服務器端已下載文檔的最近一次更新日期
string theLastsUpdateDate = GetTheLastUpdateTime(theFolder.FullName);
theFolder.FullName指在升級文檔下載到客戶機上的臨時文件夾所在的路徑。
4.3 比較日期
客戶端應用程序最近一次更新日期與服務器端升級程序的最近一次更新日期進行比較。
01 |
//獲得已下載文檔的最近一次更新日期 |
02 |
string theLastsUpdateDate = GetTheLastUpdateTime(theFolder.FullName); |
03 |
if (thePreUpdateDate != "" ) |
04 |
{ |
05 |
//如果客戶端將升級的應用程序的更新日期大於服務器端升級的應用程序的更新日期 |
06 |
if (Convert.ToDateTime(thePreUpdateDate)>=Convert.ToDateTime(theLastsUpdateDate)) |
07 |
{ |
08 |
MessageBox.Show( "當前軟件已經是最新的,無需更新!" , "系統提示" ,MessageBoxButtons.OK,MessageBoxIcon.Information); |
09 |
this .Close(); |
10 |
} |
11 |
} |
12 |
this .labDownFile.Text = "下載更新文件" ; |
13 |
this .labFileName.Refresh(); |
14 |
this .btnCancel.Enabled = true ; |
15 |
this .progressBar.Position = 0; |
16 |
this .progressBarTotal.Position = 0; |
17 |
this .progressBarTotal.Refresh(); |
18 |
this .progressBar.Refresh(); |
19 |
20 |
//通過動態數組獲取下載文件的列表 |
21 |
ArrayList List = GetDownFileList(GetTheUpdateURL(),theFolder.FullName); |
22 |
string [] urls = new string [List.Count]; |
23 |
List.CopyTo(urls, 0); |
將客戶端升級的應用程序的日期與服務器端下載的應用程序日期進行比較,如果前者大於后者,則不更新;如果前者小於后者,則通過動態數組獲取下載文件的列表,開始下載文件。
4.2 獲取客戶端應用程序及服務器端升級程序的最近一次更新日期
通過GetTheLastUpdateTime()函數來實現。
01 |
private string GetTheLastUpdateTime( string Dir) |
02 |
{ |
03 |
string LastUpdateTime = "" ; |
04 |
string AutoUpdaterFileName = Dir + @"\AutoUpdater.xml" ; |
05 |
if (!File.Exists(AutoUpdaterFileName)) |
06 |
return LastUpdateTime; |
07 |
//打開xml文件 |
08 |
FileStream myFile = new FileStream(AutoUpdaterFileName,FileMode.Open); |
09 |
//xml文件閱讀器 |
10 |
XmlTextReader xml = new XmlTextReader(myFile); |
11 |
while (xml.Read()) |
12 |
{ |
13 |
if (xml.Name == "UpdateTime" ) |
14 |
{ |
15 |
//獲取升級文檔的最后一次更新日期 |
16 |
LastUpdateTime = xml.GetAttribute( "Date" ); |
17 |
break ; |
18 |
} |
19 |
} |
20 |
xml.Close(); |
21 |
myFile.Close(); |
22 |
return LastUpdateTime; |
23 |
} |
通過XmlTextReader打開XML文檔,讀取更新時間從而獲取Date對應的值,即服務器端升級文件的最近一次更新時間。
函數調用實現:
//獲取客戶端指定路徑下的應用程序最近一次更新時間
string thePreUpdateDate = GetTheLastUpdateTime(Application.StartupPath);
Application.StartupPath指客戶端應用程序所在的路徑。
//獲得從服務器端已下載文檔的最近一次更新日期
string theLastsUpdateDate = GetTheLastUpdateTime(theFolder.FullName);
theFolder.FullName指在升級文檔下載到客戶機上的臨時文件夾所在的路徑。
4.3 比較日期
客戶端應用程序最近一次更新日期與服務器端升級程序的最近一次更新日期進行比較。
01 |
//獲得已下載文檔的最近一次更新日期 |
02 |
string theLastsUpdateDate = GetTheLastUpdateTime(theFolder.FullName); |
03 |
if (thePreUpdateDate != "" ) |
04 |
{ |
05 |
//如果客戶端將升級的應用程序的更新日期大於服務器端升級的應用程序的更新日期 |
06 |
if (Convert.ToDateTime(thePreUpdateDate)>=Convert.ToDateTime(theLastsUpdateDate)) |
07 |
{ |
08 |
MessageBox.Show( "當前軟件已經是最新的,無需更新!" , "系統提示" ,MessageBoxButtons.OK,MessageBoxIcon.Information); |
09 |
this .Close(); |
10 |
} |
11 |
} |
12 |
this .labDownFile.Text = "下載更新文件" ; |
13 |
this .labFileName.Refresh(); |
14 |
this .btnCancel.Enabled = true ; |
15 |
this .progressBar.Position = 0; |
16 |
this .progressBarTotal.Position = 0; |
17 |
this .progressBarTotal.Refresh(); |
18 |
this .progressBar.Refresh(); |
1 |
//通過動態數組獲取下載文件的列表 |
2 |
ArrayList List = GetDownFileList(GetTheUpdateURL(),theFolder.FullName); |
3 |
string [] urls = new string [List.Count]; |
4 |
List.CopyTo(urls, 0); |
將客戶端升級的應用程序的日期與服務器端下載的應用程序日期進行比較,如果前者大於后者,則不更新;如果前者小於后者,則通過動態數組獲取下載文件的列表,開始下載文件。
通過BatchDownload()函數來實現。升級程序檢測舊的主程序是否活動,若活動則關閉舊的主程序;刪除舊的主程序,拷貝臨時文件夾中的文件到相應的位置;檢查主程序的狀態,若狀態為活動的,則啟動新的主程序。
01 |
private void BatchDownload( object data) |
02 |
{ |
03 |
this .Invoke( this .activeStateChanger, new object []{ true , false }); |
04 |
try |
05 |
{ |
06 |
DownloadInstructions instructions = (DownloadInstructions) data; |
07 |
//批量下載 |
08 |
using (BatchDownloader bDL = new BatchDownloader()) |
09 |
{ |
10 |
bDL.CurrentProgressChanged += new DownloadProgressHandler( this .SingleProgressChanged); |
11 |
bDL.StateChanged += new DownloadProgressHandler( this .StateChanged); |
12 |
bDL.FileChanged += new DownloadProgressHandler(bDL_FileChanged); |
13 |
bDL.TotalProgressChanged += new DownloadProgressHandler(bDL_TotalProgressChanged); |
14 |
bDL.Download(instructions.URLs, instructions.Destination, (ManualResetEvent) this .cancelEvent); |
15 |
} |
16 |
} |
17 |
catch (Exception ex) |
18 |
{ |
19 |
ShowErrorMessage(ex); |
20 |
} |
21 |
this .Invoke( this .activeStateChanger, new object []{ false , false }); |
22 |
this .labFileName.Text = "" ; |
23 |
//更新程序 |
24 |
if ( this ._Update) |
25 |
{ |
26 |
//關閉原有的應用程序 |
27 |
this .labDownFile.Text = "正在關閉程序...." ; |
28 |
System.Diagnostics.Process[]proc=System.Diagnostics.Process.GetProcessesByName( "TIMS" ); |
29 |
//關閉原有應用程序的所有進程 |
30 |
foreach (System.Diagnostics.Process pro in proc) |
31 |
{ |
32 |
pro.Kill(); |
33 |
} |
34 |
DirectoryInfo theFolder= new DirectoryInfo(Path.GetTempPath()+"JurassicUpdate"); |
35 |
if (theFolder.Exists) |
36 |
{ |
37 |
foreach (FileInfo theFile in theFolder.GetFiles()) |
38 |
{ |
39 |
//如果臨時文件夾下存在與應用程序所在目錄下的文件同名的文件,則刪除應用程序目錄下的文件 |
40 |
if (File.Exists(Application.StartupPath + \\"+Path.GetFileName(theFile.FullName))) |
41 |
File.Delete(Application.StartupPath + "\\" +Path.GetFileName(theFile.FullName)); |
42 |
//將臨時文件夾的文件移到應用程序所在的目錄下 |
43 |
File.Move(theFile.FullName,Application.StartupPath + \\"+Path.GetFileName(theFile.FullName)); |
44 |
} |
45 |
} |
46 |
//啟動安裝程序 |
47 |
this .labDownFile.Text = "正在啟動程序...." ; |
48 |
System.Diagnostics.Process.Start(Application.StartupPath + "\\" + "TIMS.exe" ); |
49 |
this .Close(); |
50 |
} |
51 |
} |