好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

程序自动更新版本

程序自动更新版本

增加了程序自动更新版本的功能,实现方式如下:

后台数据库中用一张表来保存程序的版本信息,该表的字段很简单,如下:

 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                      }

如果版本低于服务器上面的版本,那么则启动更新程序:

待更新结束之后再重新打开最新版本的程序,主程序和更新程序放在同一目录里面。


 

 

分类:  C# ,  WinForm

标签:  WinForm ,  框架

作者: Leo_wl

    

出处: http://www.cnblogs.com/Leo_wl/

    

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

版权信息

查看更多关于程序自动更新版本的详细内容...

  阅读:40次