自动升级程序框架描述
2007-06-13 17:22
411 查看
最近考虑.NET下自动升级程序的问题,到网上搜索了一下,现将自动升级程序框架描述及c#demo实现汇总如下。该框架的描述入图所示。
![](http://p.blog.csdn.net/images/p_blog_csdn_net/FuWaer/edf2ee8f74324a3a86e8dd582854d52d.png)
图:自动升级程序框架描述图
服务器端程序,该程序与自动升级程序无关,之所以把它画出来,是为了图的完整性,用Server.exe表示
升级服务端程序,该程序负责监听升级客户端程序的连接请求,用UpdateServer.exe表示
升级客户端程序,该程序与客户端程序位于相同的主机上,负责与升级服务端程序通信并对客户端程序进行升级操作,用UpdateClient.exe表示
升级客户端程序代理,该代理以dll形式提供给客户端程序使用,该代理负责与启动升级客户端程序并传递相关信息给升级客户端程序,用UpdateClientProxy.dll表示
客户端程序主程序启动时,使用升级客户端程序代理启动升级客户端程序并将客户端程序的当前版本号和其它相关的信息传递给升级客户端程序,由于该操作是异步并行的,因此,客户端程序不需要等待结果即可继续运行当前版本的程序,如果当升级客户端发现新版本时会以消息的形式通知客户端程序。
启动起来的升级客户端程序接收来自客户端程序传递的当前版本号和其它相关信息(比如客户端程序所在目录等),升级客户端程序同升级服务端程序通过socket建立连接,并将当前版本号传递给升级服务端程序,由升级服务端程序告知当前是否有新版本,如果没有,则退出;如果有,从升级服务端程序获得需要更新的文件,保存在本地的临时文件夹中,下载完毕后,通知客户端程序有新版本,如果有必要,升级客户端程序将结束正在运行的客户端程序,将下载得到的新版本文件复制替换到客户端程序所在目录,然后升级客户端程序从新启动客户端程序,实现一次自动更新的过程。
升级服务端程序是个常驻内存的服务,实时监听来自升级客户端程序的连接请求,判断是否需要提供给升级客户端程序新版本的程序文件,即,当接收到升级客户端传送的版本号时,升级服务端程序将查询本地的配置文件,比较觉得是否有新版本程序,如果没有则告知升级客户端,如果有,则将已经存储在本地特定目录下的新版本文件通过约定好的协议,传递给升级客户端。
使用内存映像文件机制实现升级客户端代理向升级客户端程序传递数据。
参见《.NET进程通信机制——内存映像文件》
3.1.2自定义消息
导入win32api,SendMessage
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
[DllImport("User32.dll", EntryPoint="SendMessage")]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public static extern int SendMessage (IntPtr hwnd, int wmsg, IntPtr wparam, IntPtr lparam);
建立消息类
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class Message
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const int USER = 0x0400;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const int WM_TEST = USER+101;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const int WM_CLOSE = 0x0010;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
发送自定义消息给指定窗体
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public void SendMessage()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Win32.SendMessage(hFormHandle,Message.WM_TEST,IntPtr.Zero,IntPtr.Zero);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
窗体接收自定义消息,需要重载WndProc
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
protected override void WndProc(ref Message m)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
switch(m.Msg)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case Message.WM_TEST:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//处理该自定消息的代码
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
base.WndProc (ref m);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
3.1.3 C#下代码调用外部程序
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
System.Diagnostics.Process proc = System.Diagnostics.Process.Start(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"d://ConsoleApplication2//bin/Debug//ConsoleApplication2.exe");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Threading;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Runtime.InteropServices;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace UpdateClient
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// Class1 的摘要说明。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public class UpdateClientProxy
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static string _exeVersion = "";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static string _exePhysicalPath = "";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static IntPtr _exeHandle = IntPtr.Zero;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static Thread _updateProc = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static void BeginUpdate( string exeVersion,string exePhysicalPath,IntPtr exeHandle )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_exeVersion = exeVersion;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_exePhysicalPath = exePhysicalPath;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_exeHandle = exeHandle;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_updateProc = new Thread(new ThreadStart(UpdateProc));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_updateProc.Start();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static void UpdateProc()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IntPtr hVersion = CreateFileMapping( "exeVersion" );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IntPtr hPhysicalPath = CreateFileMapping( "exePhysicalPath" );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IntPtr hHandle = CreateFileMapping( "exeHandle" );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
WriteMapView( hVersion,_exeVersion );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
WriteMapView( hPhysicalPath,_exePhysicalPath );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
WriteMapView( hHandle,_exeHandle.ToString() );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.Diagnostics.Process proc = System.Diagnostics.Process.Start(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
"d:/ConsoleApplication2/bin/Debug/ConsoleApplication2.exe");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static IntPtr CreateFileMapping( string name )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IntPtr hFileMap = IntPtr.Zero;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hFileMap = Win32.CreateFileMapping(Win32.InvalidHandleValue,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IntPtr.Zero, Win32.PAGE_READWRITE,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0, 0x100, name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (hFileMap == IntPtr.Zero)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw new Exception("CreateFileMapping():无法创建内存映像文件");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return hFileMap;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static void WriteMapView( IntPtr hFileMap,string strValue )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IntPtr address = Win32.MapViewOfFile(hFileMap, Win32.FILE_MAP_WRITE,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0, 0, IntPtr.Zero);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] bs = System.Text.Encoding.GetEncoding("gb2312").GetBytes(strValue);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i=0;i<bs.Length;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Marshal.WriteByte(address,i,bs[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Marshal.WriteByte(address,bs.Length,0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Win32.UnmapViewOfFile(address);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static string ReadMapView( IntPtr hFileMap )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IntPtr address = Win32.MapViewOfFile(hFileMap, Win32.FILE_MAP_WRITE,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0, 0, IntPtr.Zero);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] bs = new byte[0x100];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(i=0;i<bs.Length;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte temp = Marshal.ReadByte(address,i);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(temp==0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bs[i] = temp;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Win32.UnmapViewOfFile(address);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return System.Text.Encoding.GetEncoding("gb2312").GetString(bs,0,i);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
internal class Win32
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[DllImport("User32.dll",EntryPoint="FindWindow")]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static extern IntPtr FindWindow(string lpClassName,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string lpWindowName);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[DllImport("User32.dll", EntryPoint="SendMessage")]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static extern int sendmessage (IntPtr hwnd, int wmsg, IntPtr wparam, IntPtr lparam);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[DllImport("User32.dll", EntryPoint="SendMessage")]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static extern int sendmessage (IntPtr hwnd, int wmsg, IntPtr wparam, string lparam);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static readonly IntPtr InvalidHandleValue = new IntPtr(-1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const UInt32 FILE_MAP_WRITE = 2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const UInt32 PAGE_READWRITE = 0x04;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[DllImport("Kernel32")]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static extern IntPtr CreateFileMapping(IntPtr hFile,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IntPtr pAttributes, UInt32 flProtect,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
UInt32 dwMaximumSizeHigh, UInt32 dwMaximumSizeLow, String pName);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[DllImport("Kernel32")]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static extern IntPtr MapViewOfFile(IntPtr hFileMappingObject,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
UInt32 dwDesiredAccess,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
UInt32 dwFileOffsetHigh, UInt32 dwFileOffsetLow,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IntPtr dwNumberOfBytesToMap);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[DllImport("Kernel32")]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static extern Boolean UnmapViewOfFile(IntPtr address);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
3.2.2模拟的客户端程序
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void Form1_Load(object sender, System.EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
UpdateClient.UpdateClientProxy.BeginUpdate(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
"1.0.0.1",
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
AppDomain.CurrentDomain.BaseDirectory+AppDomain.CurrentDomain.FriendlyName,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Handle
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
protected override void WndProc(ref Message m)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
switch(m.Msg)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case Message.WM_TEST:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
new System.Threading.Thread(new System.Threading.ThreadStart(DemoProc)).Start();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
base.WndProc (ref m);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void DemoProc()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.textBox1.Text = "升级程序检测到有新版本,即将关闭应用程序并进行升级操作!";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.Threading.Thread.Sleep(5000);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
3.2.3模拟的升级客户端程序
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
[STAThread]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
static void Main(string[] args)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("模拟的升级客户端程序已经被启动");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IntPtr hVersion = FileMapping.CreateFileMapping( "exeVersion" );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IntPtr hPhysicalPath = FileMapping.CreateFileMapping( "exePhysicalPath" );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IntPtr hHandle = FileMapping.CreateFileMapping( "exeHandle" );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string _version = FileMapping.ReadMapView( hVersion );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string _physicalpath = FileMapping.ReadMapView( hPhysicalPath );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string _handle = FileMapping.ReadMapView( hHandle );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_version!=null&&_version!="" &&
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_physicalpath!=null&&_physicalpath!="" &&
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_handle!=null&&_handle!="" )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("接收到程序传送的版本号:{0}",_version);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("接收到程序传送的物理路径:{0}",_physicalpath);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("正在连接升级服务器......");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("连接到升级服务器.");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("获取升级列表");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("正在下载文件a.dll");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("正在下载文件b.dll");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("正在下载文件abc.xml");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("已经成功下载全部所需的更新文件");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("通知主程序关闭自己");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Win32.sendmessage(new IntPtr(Convert.ToInt32(_handle)),Message.WM_TEST,IntPtr.Zero,IntPtr.Zero);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.Threading.Thread.Sleep(5000);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("主程序已经关闭,升级客户端程序将进行文件替换");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("正在替换a.dll");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("正在替换b.dll");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("正在替换abc.xml");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("所有更新文件全部替换成功");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("升级客户端程序将重新启动主程序");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.Diagnostics.Process.Start(_physicalpath);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://p.blog.csdn.net/images/p_blog_csdn_net/FuWaer/edf2ee8f74324a3a86e8dd582854d52d.png)
图:自动升级程序框架描述图
1框架的5个部分
客户端程序,即需要被升级的程序,在下图中用Client.exe表示服务器端程序,该程序与自动升级程序无关,之所以把它画出来,是为了图的完整性,用Server.exe表示
升级服务端程序,该程序负责监听升级客户端程序的连接请求,用UpdateServer.exe表示
升级客户端程序,该程序与客户端程序位于相同的主机上,负责与升级服务端程序通信并对客户端程序进行升级操作,用UpdateClient.exe表示
升级客户端程序代理,该代理以dll形式提供给客户端程序使用,该代理负责与启动升级客户端程序并传递相关信息给升级客户端程序,用UpdateClientProxy.dll表示
2自动升级程序思路
自动升级程序的设计思路比较清晰,遵循如下步骤进行:客户端程序主程序启动时,使用升级客户端程序代理启动升级客户端程序并将客户端程序的当前版本号和其它相关的信息传递给升级客户端程序,由于该操作是异步并行的,因此,客户端程序不需要等待结果即可继续运行当前版本的程序,如果当升级客户端发现新版本时会以消息的形式通知客户端程序。
启动起来的升级客户端程序接收来自客户端程序传递的当前版本号和其它相关信息(比如客户端程序所在目录等),升级客户端程序同升级服务端程序通过socket建立连接,并将当前版本号传递给升级服务端程序,由升级服务端程序告知当前是否有新版本,如果没有,则退出;如果有,从升级服务端程序获得需要更新的文件,保存在本地的临时文件夹中,下载完毕后,通知客户端程序有新版本,如果有必要,升级客户端程序将结束正在运行的客户端程序,将下载得到的新版本文件复制替换到客户端程序所在目录,然后升级客户端程序从新启动客户端程序,实现一次自动更新的过程。
升级服务端程序是个常驻内存的服务,实时监听来自升级客户端程序的连接请求,判断是否需要提供给升级客户端程序新版本的程序文件,即,当接收到升级客户端传送的版本号时,升级服务端程序将查询本地的配置文件,比较觉得是否有新版本程序,如果没有则告知升级客户端,如果有,则将已经存储在本地特定目录下的新版本文件通过约定好的协议,传递给升级客户端。
3 C#实现
3.1主要技术细节
3.1.1进程内通信使用内存映像文件机制实现升级客户端代理向升级客户端程序传递数据。
参见《.NET进程通信机制——内存映像文件》
3.1.2自定义消息
导入win32api,SendMessage
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
[DllImport("User32.dll", EntryPoint="SendMessage")]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public static extern int SendMessage (IntPtr hwnd, int wmsg, IntPtr wparam, IntPtr lparam);
建立消息类
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class Message
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const int USER = 0x0400;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const int WM_TEST = USER+101;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const int WM_CLOSE = 0x0010;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
发送自定义消息给指定窗体
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public void SendMessage()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Win32.SendMessage(hFormHandle,Message.WM_TEST,IntPtr.Zero,IntPtr.Zero);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
窗体接收自定义消息,需要重载WndProc
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
protected override void WndProc(ref Message m)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
switch(m.Msg)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case Message.WM_TEST:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//处理该自定消息的代码
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
base.WndProc (ref m);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
3.1.3 C#下代码调用外部程序
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
System.Diagnostics.Process proc = System.Diagnostics.Process.Start(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"d://ConsoleApplication2//bin/Debug//ConsoleApplication2.exe");
3.2演示程序代码
3.2.1升级客户端代理类![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Threading;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Runtime.InteropServices;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace UpdateClient
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// Class1 的摘要说明。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public class UpdateClientProxy
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static string _exeVersion = "";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static string _exePhysicalPath = "";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static IntPtr _exeHandle = IntPtr.Zero;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static Thread _updateProc = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static void BeginUpdate( string exeVersion,string exePhysicalPath,IntPtr exeHandle )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_exeVersion = exeVersion;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_exePhysicalPath = exePhysicalPath;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_exeHandle = exeHandle;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_updateProc = new Thread(new ThreadStart(UpdateProc));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_updateProc.Start();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static void UpdateProc()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IntPtr hVersion = CreateFileMapping( "exeVersion" );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IntPtr hPhysicalPath = CreateFileMapping( "exePhysicalPath" );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IntPtr hHandle = CreateFileMapping( "exeHandle" );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
WriteMapView( hVersion,_exeVersion );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
WriteMapView( hPhysicalPath,_exePhysicalPath );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
WriteMapView( hHandle,_exeHandle.ToString() );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.Diagnostics.Process proc = System.Diagnostics.Process.Start(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
"d:/ConsoleApplication2/bin/Debug/ConsoleApplication2.exe");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static IntPtr CreateFileMapping( string name )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IntPtr hFileMap = IntPtr.Zero;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hFileMap = Win32.CreateFileMapping(Win32.InvalidHandleValue,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IntPtr.Zero, Win32.PAGE_READWRITE,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0, 0x100, name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (hFileMap == IntPtr.Zero)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw new Exception("CreateFileMapping():无法创建内存映像文件");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return hFileMap;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static void WriteMapView( IntPtr hFileMap,string strValue )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IntPtr address = Win32.MapViewOfFile(hFileMap, Win32.FILE_MAP_WRITE,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0, 0, IntPtr.Zero);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] bs = System.Text.Encoding.GetEncoding("gb2312").GetBytes(strValue);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i=0;i<bs.Length;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Marshal.WriteByte(address,i,bs[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Marshal.WriteByte(address,bs.Length,0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Win32.UnmapViewOfFile(address);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static string ReadMapView( IntPtr hFileMap )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IntPtr address = Win32.MapViewOfFile(hFileMap, Win32.FILE_MAP_WRITE,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0, 0, IntPtr.Zero);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] bs = new byte[0x100];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(i=0;i<bs.Length;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte temp = Marshal.ReadByte(address,i);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(temp==0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bs[i] = temp;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Win32.UnmapViewOfFile(address);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return System.Text.Encoding.GetEncoding("gb2312").GetString(bs,0,i);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
internal class Win32
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[DllImport("User32.dll",EntryPoint="FindWindow")]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static extern IntPtr FindWindow(string lpClassName,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string lpWindowName);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[DllImport("User32.dll", EntryPoint="SendMessage")]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static extern int sendmessage (IntPtr hwnd, int wmsg, IntPtr wparam, IntPtr lparam);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[DllImport("User32.dll", EntryPoint="SendMessage")]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static extern int sendmessage (IntPtr hwnd, int wmsg, IntPtr wparam, string lparam);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static readonly IntPtr InvalidHandleValue = new IntPtr(-1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const UInt32 FILE_MAP_WRITE = 2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const UInt32 PAGE_READWRITE = 0x04;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[DllImport("Kernel32")]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static extern IntPtr CreateFileMapping(IntPtr hFile,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IntPtr pAttributes, UInt32 flProtect,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
UInt32 dwMaximumSizeHigh, UInt32 dwMaximumSizeLow, String pName);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[DllImport("Kernel32")]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static extern IntPtr MapViewOfFile(IntPtr hFileMappingObject,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
UInt32 dwDesiredAccess,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
UInt32 dwFileOffsetHigh, UInt32 dwFileOffsetLow,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IntPtr dwNumberOfBytesToMap);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[DllImport("Kernel32")]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static extern Boolean UnmapViewOfFile(IntPtr address);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
3.2.2模拟的客户端程序
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void Form1_Load(object sender, System.EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
UpdateClient.UpdateClientProxy.BeginUpdate(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
"1.0.0.1",
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
AppDomain.CurrentDomain.BaseDirectory+AppDomain.CurrentDomain.FriendlyName,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Handle
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
protected override void WndProc(ref Message m)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
switch(m.Msg)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case Message.WM_TEST:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
new System.Threading.Thread(new System.Threading.ThreadStart(DemoProc)).Start();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
base.WndProc (ref m);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void DemoProc()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.textBox1.Text = "升级程序检测到有新版本,即将关闭应用程序并进行升级操作!";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.Threading.Thread.Sleep(5000);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
3.2.3模拟的升级客户端程序
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
[STAThread]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
static void Main(string[] args)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("模拟的升级客户端程序已经被启动");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IntPtr hVersion = FileMapping.CreateFileMapping( "exeVersion" );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IntPtr hPhysicalPath = FileMapping.CreateFileMapping( "exePhysicalPath" );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IntPtr hHandle = FileMapping.CreateFileMapping( "exeHandle" );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string _version = FileMapping.ReadMapView( hVersion );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string _physicalpath = FileMapping.ReadMapView( hPhysicalPath );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string _handle = FileMapping.ReadMapView( hHandle );
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_version!=null&&_version!="" &&
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_physicalpath!=null&&_physicalpath!="" &&
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_handle!=null&&_handle!="" )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("接收到程序传送的版本号:{0}",_version);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("接收到程序传送的物理路径:{0}",_physicalpath);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("正在连接升级服务器......");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("连接到升级服务器.");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("获取升级列表");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("正在下载文件a.dll");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("正在下载文件b.dll");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("正在下载文件abc.xml");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("已经成功下载全部所需的更新文件");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("通知主程序关闭自己");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Win32.sendmessage(new IntPtr(Convert.ToInt32(_handle)),Message.WM_TEST,IntPtr.Zero,IntPtr.Zero);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.Threading.Thread.Sleep(5000);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("主程序已经关闭,升级客户端程序将进行文件替换");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("正在替换a.dll");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("正在替换b.dll");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("正在替换abc.xml");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("所有更新文件全部替换成功");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("升级客户端程序将重新启动主程序");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.Diagnostics.Process.Start(_physicalpath);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
相关文章推荐
- Linux软件上电启动自动升级程序框架
- 用PB实现客户端程序的自动升级
- 用PB实现客户端程序的自动升级
- silverlight4 程序升级到 5,关闭子窗口,自动把父窗口变灰失效,对鼠标无反应的解决办法
- [python] 3 、基于串口通信的嵌入式设备上位机自动测试程序框架(简陋框架)
- Android应用的自动升级、更新模块的实现完整方案+参考程序
- C#Winform程序如何发布并自动升级(图解)
- C#基于FTP协议的简易软件自动升级程序
- 利用ant脚本 自动构建svn增量/全量 系统程序升级包
- 利用ant脚本 自动构建svn增量/全量 系统程序升级包
- 基于TCP网络通信的自动升级程序源码分析--生成升级文件相关的配置文件
- ASP 程序实现自动升级功能
- 让我们自己的Android程序可以自动后台升级的实现(需Root权限)
- C#编写软件自动升级更新程序
- 基于QUpdater组件为你的WinForm程序添加自动升级功能
- [IT]C/S自动升级程序原理
- 自动升级程序
- ASP 程序实现自动升级功能
- 软件自动升级程序的设计思路
- c#+handle.exe实现升级程序在运行时自动解除文件被占用的问题