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 ; } }
其中配置文件里配置相关的路径等 -
-