很多软件都支持自动升级,WINDOWS自身也有自动更新的功能。不知道哪位大侠知道自动升级的原理是什么?用到了什么技术?
有源码的就最好了,方便的话给我一份 小弟将不胜感激 [email protected]
有源码的就最好了,方便的话给我一份 小弟将不胜感激 [email protected]
解决方案 »
- 请问,在UI线程之外的线程访问控件
- 急!ArrayList的元素是类对象,Contains怎样判断元素是否已存在?
- c#中为什么要用到托管技术,在什么情况会用到托管技术?一直搞不明白
- 关于接口的问题,有什么用?
- 在线等啊 ultratree或treeview的编辑问题
- 如何让一个ToolStrip漂浮起来?
- 帮忙改一段代码,谢谢
- 在线等!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!11
- MdiParent 問題
- 关于c#编写的聊天软件,服务端在具有公网ip一端,客户端在不同的内网,那么不同的客户端之间如何进行通讯?
- .net如何构建分布式系统?跪求大家指点一二
- winform打印问题?如何去掉"正在打印"提示窗口,直接打印
1,验证版本
2,下载要更新的文件,单独EXE操作这块,以方便后边的关闭程序,重新打开
3,下载完毕后,重新打开程序
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() );
}
}
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
}
}
Application Auto Update Revisited
http://www.codeproject.com/KB/vb/Auto_Update_Revisited.aspxApplication Auto Update in VB.NET
http://www.codeproject.com/KB/vb/autoupdate.aspx
都有源码的
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;
}
}
其中配置文件里配置相关的路径等
我现在用的方法是:
服务端一个配置文件,客户端一个配置文件
程序启运时读取客户端的配置文件版本号与服务器配置文件的版本号对比,如果不一样就下载服务器的(服务端是将配置文件和要下载的文件那一个虚拟目录)我下载文件格式是一个压缩包(包括配置文件一同下载下来),下到客户端后新建一个临时然后自动解压,待点击【完成】后,将解压的文件Copy覆盖客户端对应的文件(注意配置文件最后复制,如果你配置文件先复制的话中间出现异常,其它文件没复制,这时客户端配置文件与服务端配置文件一样了,下次不会自动更新,最后复制就不会有这个问题),
1.读取服务器上xml文件,里面有版本信息和需要下载的文件列表.
2.验证版本,下载相应文件,我比较懒,版本一不对就全下载,楼主可以对各个文件单独处理.
3.打开主程序.
TmSendStatusRequest.Stop()
Dim ReturnValue As Boolean = False
Try
If (IsConnected = True) Then
ReturnValue = StatusRequest()
Exit Try
End If
Catch ex As Exception
EventLog.WriteEntry("PVGAFDS\TmSendStatusRequest_Elapsed", ex.Message.ToString() + Constants.vbCrLf + ex.StackTrace.ToString(), EventLogEntryType.Error)
Finally
End Try
TmSendStatusRequest.Start()
End Sub
TmSendStatusRequest.Stop()
Dim ReturnValue As Boolean = False
Try
If (IsConnected = True) Then
ReturnValue = StatusRequest()
Exit Try
End If
Catch ex As Exception
EventLog.WriteEntry("PVGAFDS\TmSendStatusRequest_Elapsed", ex.Message.ToString() + Constants.vbCrLf + ex.StackTrace.ToString(), EventLogEntryType.Error)
Finally
End Try
TmSendStatusRequest.Start()
End Sub
http://www.cnblogs.com/jinliangliu/archive/2009/02/26/1398754.html
//WINDOWS模拟升级
//升级程序.
private void button1_Click(object sender, EventArgs e)
{
Process[] ps = Process.GetProcesses();
string path = "";
foreach (Process var in ps)
{
if (var.ProcessName == "WindowsApplication2")
{
path = var.MainModule.FileName;
var.Kill();
break;
}
}
MessageBox.Show(path);
if (delfile(path))
{
File.Delete(path);
File.Copy(@"D:\temp\系统升级\newversion\WindowsApplication2\WindowsApplication2\bin\Debug\WindowsApplication2.exe", path);
Process.Start(path);
}
} static bool delfile(string path)
{
bool tag = false;
AppDomain ad = AppDomain.CreateDomain("dll test");
myclass obj = (myclass)ad.CreateInstanceFromAndUnwrap(@"update.exe", "update.myclass");
if (obj.LoadAssembly(path))
{ tag = true;}
AppDomain.Unload(ad);
obj = null;
return tag;
}
} public class myclass : MarshalByRefObject
{
public bool LoadAssembly(string asspath)
{
bool tag=false;
Assembly assembly = Assembly.LoadFrom(asspath);
if (assembly.GetName().Version.ToString() == "1.0.0.0")
tag = true;
return tag;
} }///////////////////////////
原始版本:1.0版本的WindowsApplication2.exe
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("this is version 1.0");
}//////////////////////////////
升级后的版本:2.0版本的WindowsApplication2.exe
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("this is version 2.0");
}
需要就将要更新的文件通过webclient下载到本地再更新安装路径下的文件,删除下载的文件即可