第二步:配置本地的Config
經過第一步的流程,這一步要做的就是配置本地的Config用于監測并下載遠程IIS或者FTP下需要更新的文件,具體如下圖所示:
圖3
第三步:修改主程序復制代碼 代碼如下:
#region check and download new version program
bool bHasError = false;
IAutoUpdater autoUpdater = new AutoUpdater();
try
{
autoUpdater.Update();
}
catch (WebException exp)
{
MessageBox.Show("Can not find the specified resource");
bHasError = true;
}
catch (XmlException exp)
{
bHasError = true;
MessageBox.Show("Download the upgrade file error");
}
catch (NotSupportedException exp)
{
bHasError = true;
MessageBox.Show("Upgrade address configuration error");
}
catch (ArgumentException exp)
{
bHasError = true;
MessageBox.Show("Download the upgrade file error");
}
catch (Exception exp)
{
bHasError = true;
MessageBox.Show("An error occurred during the upgrade process");
}
finally
{
if (bHasError == true)
{
try
{
autoUpdater.RollBack();
}
catch (Exception)
{
//Log the message to your file or database
}
}
}
#endregion使用就是這么簡單,更詳細的操作,大家可以下載源碼,也正因為它的簡單,所以大家可以對其修改以滿足具體項目的需求。
當我們運行主程序(WinForm或者WPF),如果服務器上有最新的版本,就會彈出如下頁面進行提示并讓用戶選擇是否更新。
圖4
當用戶不需要更新時,可以選擇Skip按鈕跳過并繼續主程序流程,反之則進入如下頁面。
圖5
在下載的過程中,用戶可以選擇Cancel停止下載并重新回到主流程。
七. 維護與下載該組件已經托管到CodePlex,所以大家可以到上面下載其源代碼,具體地址:
圖6
八. 總結 我們這篇文章簡單的講解了一個簡單的自動更新組件,由于它比較簡單而且功能單一,所以沒有花費大量的篇幅進行論述,感興趣的讀者可以下載其源碼。后面我會把自己開發的一套MVVM框架也托管上去,由于目前對Silverlight的支持還不太好,所以正在持續做相關的改進,其中借鑒了一些其他優秀框架的思想并且融入了一些實際應用場景,所以到時也希望大家能夠多多指教!
作者:圣殿騎士
出處:
新聞熱點
疑難解答
圖片精選