自动更新版本

1,验证版本
C# code
    
    
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault( false ); /* 2,4,6 检查升级 */ if ( ( DateTime.Now.DayOfWeek == DayOfWeek.Tuesday || DateTime.Now.DayOfWeek == DayOfWeek.Thursday || DateTime.Now.DayOfWeek == DayOfWeek.Saturday ) && Version.CheckVersion() ) { // 升级 UpdateVersion frm = new UpdateVersion(); if ( frm.ShowDialog() == DialogResult.OK ) { Application.Run( new TstringCodeFactory() ); } } else { // 打开主窗口 Application.Run( new TstringCodeFactory() ); } }
2,下载要更新的文件
    
    
C# code
       
       
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Xml; using Tstring.Core.Util; using System.Configuration; using System.Diagnostics; using System.IO; namespace Tstring.Core.Builder { public partial class UpdateVersion : Form { #region field string fileName = " Tstring.Core.Builder.exe " ; #endregion #region property public int Version { get ; set ; } #endregion #region constructor public UpdateVersion() { InitializeComponent(); this .StartPosition = FormStartPosition.CenterScreen; this .FormBorderStyle = FormBorderStyle.None; this .ShowInTaskbar = true ; this .Text = " 自动更新 " ; } #endregion #region event protected override void OnLoad( EventArgs e ) { FileDownLoad file = new FileDownLoad( ConfigurationManager.AppSettings[ " UpdateExe " ], fileName ); file.OnProgress += new DownloadProgressHandler( file_OnProgress ); file.OnComplete += new DownloadCompleteHandler( file_OnComplete ); file.Start(); base .OnLoad( e ); } void file_OnComplete( string filename, byte [] data ) { if ( data != null ) { if ( ! Directory.Exists( " bak " ) ) Directory.CreateDirectory( " bak " ); if ( File.Exists( fileName ) ) File.Move( fileName, Guid.NewGuid().ToString() + " .exe " ); using ( FileStream stream = new FileStream( fileName, FileMode.CreateNew ) ) { using ( BinaryWriter writefile = new BinaryWriter( stream ) ) { writefile.Write( data ); } } } this .DialogResult = DialogResult.OK; } void file_OnProgress( string filename, int totalBytes, int bytesRead ) { this .Invoke( new Action( () => { this .label1.Text = string .Format( " 正在更新:{0}/{1} bytes " , bytesRead, totalBytes ); } ) ); } #endregion } }
这里是验证版本
C# code
                   
                   
public class Version { public static int Verstion { get ; set ; } public static bool CheckVersion() { string path = System.Configuration.ConfigurationManager.AppSettings[ " UpdatePath " ]; string version = System.Configuration.ConfigurationManager.AppSettings[ " Version " ]; if ( ! string .IsNullOrEmpty( path ) ) { XmlDocument xml = new XmlDocument(); try { xml.Load( path ); XmlNode node = xml.SelectSingleNode( " /version " ); if ( node != null ) { System.Configuration.ConfigurationManager.AppSettings[ " Version " ] = node.InnerText; return node.InnerText != version; } } catch (Exception exp) { Log.Error( exp ); return false ; } } return false ; } }



其中配置文件里配置相关的路径等
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值