天天看点

软件自动更新功能的实现

今天一朋友在群里面问,软件自动更新功能怎么做,大家都不知道怎么搞,我下午刚好没事情,就研究了下。

附上我的源代码 考虑下基本的思路 1

、客户端(主程序)调用升级程序,升级程序连接到最新的服务器上。

2、升级程序获取服务器上的xml配置文件中最新程序的更新日期或版本号或文件大小等。

3、升级程序获取客户端(主程序)的xml配置文件的更新日期或版本号大小等,然后两者进行比较;

如果如果新版本日期>原有程序的最新日期,则提示用户是否升级;

或如果新版本版本号>原有程序的版本号,则提示用户是否升级;

再或如果新版本文件大小>原有程序的文件大小,则提示用户是否升级。

本文主要采用一般的做法,就是通过版本号来进行对比。

4、如果用户选择升级,则下载文件列表。

5.在本地建立与远程IIS或者FTP相应的临时目录,然后下载到这个临时目录文件下;

6.删除旧的主程序,拷贝临时文件夹中的文件到相应的位置;

8.结束升级流程并重新启动主程序。

参考了下网络下的代码,有比较好的封装好的dll文件,感觉博客园的圣殿骑士写的自动更新组件还是相当不错的,而且还是开源的(详情参考http://www.cnblogs.com/KnightsWarrior/archive/2010/10/20/1856255.html)。

通过这套组件,升级就相当简单咯 我在服务端创建了一个xml文件(updateservice.xml)

<?xml version="1.0" encoding="utf-8" ?> 
<updateFiles>   
    <file path="AspNetPager.dll" url="http://localhost:6137/UpdateDemo/Asp NetPager.dll" lastver="1.2.0.0" size="122880" needRestart="true" />   
    <file path="Client.exe" url="http://localhost:6137/UpdateDemo/Client.exe" lastver="1.2.0.0" size="9728" needRestart="true" /> 
</updateFiles>      

在客户端的(\UpdateApp\Client\bin\Debug)下创建了一个AutoUpdater.config文件,(注意该文件名字是自动升级组件类库中的默认值,你可以修改类库源代码的该值来改为你想要的名字(该值在/AutoUpdater/AutoUpdateHelper/ConstFile.cs 文件内))

<?xml version="1.0" encoding="utf-8"?> 
<Config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <Enabled>true</Enabled> 
    <ServerUrl>http://localhost:6137/updateservice.xml</ServerUrl> 
    <UpdateFileList> 
        <LocalFile path="Client.exe" lastver="1.0.0.0" size="9200" /> 
    </UpdateFileList> 
</Config>      

我做个非常简单的window form窗体应用

  在“检查更新”按钮做了个简单的事件。

/// <summary>         
/// 检查更新         
/// </summary>         
/// <param name="sender"></param>         
/// <param name="e"></param>
         private void button1_Click(object sender, EventArgs e)
                  {             
                  #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         
                }      

就这样,ok了,点击就能自动检测更新咯,嘿嘿