背景:
假如已经开发好了一个管理系统,如OverseaPMS.exe,现在有个需求,希望以后程序有变更时,客户端可以检测到,然后从服务器更新高版本程序到本地,而不用开发人员打包setup.exe发给用户覆盖安装。
思路:
打开程序OverseaPMS.exe后,分别读取本地配置文件SystemConfig.xml和服务器配置文件ServerUpdate.xml(一般是在登录界面读取配置文件,如果没有就在主程序界面),从本地配置文件中读取客户端版本号ClientVersion,将其与服务器上的版本号ServerVersion比较。如果ClientVersion小于ServerVersion,就调用更新程序AutoUpdate.exe进行更新,否则就继续运行本地程序OverseaPMS.exe。
具体实施:
(1)客户端程序:
1. OverseaPMS.exe程序代码,内部设置检测机制,思路如上文。
注:检查更新通过异步实现:BackgroundWorker。
// 开始执行后台操作(耗时操作)
this.backgroundUpdate.RunWorkerAsync();//触发backgroundUpadate_DoWork方法执行
在App.config中配置 <!--更新路径-->
<add key="UpdatePath" value="http://localhost:8090/Update_OverseaPMS" />
#region 检查更新
/// <summary>
/// 检测更新,检测MAC地址
/// </summary>
private void backgroundUpadate_DoWork(object sender, DoWorkEventArgs e)
{
try
{
e.Result = false;
string localxmlPath = System.Windows.Forms.Application.StartupPath + "\\SystemConfig.xml";//ClientUpdate.xml
//string ct = System.Windows.Forms.Application.StartupPath + "\\setup.exe";//OverseaPMS.exe
string serverpath = basePath + "/ServerUpdate.xml";//basePath为更新路径,在App.config中配置
System.Xml.XmlDocument dc = new XmlDocument();
dc.Load(localxmlPath);
string clientVer = dc.SelectSingleNode("/SystemConfig/ClientUpdate/Version").InnerText;
double cv = Convert.ToDouble(clientVer);
System.Xml.XmlDocument dc1 = new XmlDocument();
dc1.Load(serverpath);//
string serVer = dc1.SelectSingleNode("/ServerUpdate/Version").InnerText;
double sv = Convert.ToDouble(serVer);
// MessageBox.Show("获取版本号!");
if (cv < sv)
{
e.Result = true;
return;
}
else
{
//MessageBox.Show("当前版本为最新!");
e.Result = false;
return;
}
}
catch (FileNotFoundException fx)
{
MessageBox.Show("找不到客户端版本更新文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (System.Exception ex)
{
Utility.Log.WriteSysLog("Main," + MethodBase.GetCurrentMethod().Name, ex);
MessageBox.Show("网络连接异常或操作不成功,请重试或与管理员联系!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void backgroundUpadate_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
try
{
if ((bool)e.Result)
{
// return; //测试使用
if (MessageBox.Show("检测到有更高版本!是否进行更新", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
//MessageBox.Show("Completed!");
System.Diagnostics.Process.Start(Application.StartupPath + "\\Update\\AutoUpdate.exe", Application.StartupPath.Replace(' ', '|'));
System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcesses();
foreach (System.Diagnostics.Process p in ps)
{
if (p.ProcessName.ToLower() == "overseapms" || p.ProcessName.ToLower() == "overseapms.vshost")
{
//MessageBox.Show(p.ProcessName);
p.Kill();
break;
}
}
}
}
}
catch (System.Exception ex)
{
Utility.Log.WriteSysLog("Main," + MethodBase.GetCurrentMethod().Name, ex);
MessageBox.Show("网络连接异常或操作不成功,请重试或与管理员联系!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
#endregion
2. 开发自动更新程序AutoUpdate.exe,供OverseaPMS.exe调用。
更正:
(2)服务器:
1. 新建一个文件夹Update,存放要更新的文件如高版本的.exe程序,.dll,ServerUpdate.xml等。
2. 将文件夹Update拷贝至服务器。
如下图,我是在服务器桌面新建了一个test文件夹,并将Update文件夹拷贝进入了。
3. IIS添加网站,网站名可以随便填,目录指向test。
转:ASP.NET程序发布、IIS安装、在IIS上部署网站等
部署后注意:网站需要配置.config,否则客户端无法从服务器下载.exe.config文件,更新后的程序不能运行。