winform应用程序自动更新版本
来源:互联网 发布:c语言专家电子书 编辑:程序博客网 时间:2024/05/28 23:21
我现在做的是由更新程序来更新winform程序,但是最后的目的是想winform自己本身能够更新吧,还是没找到好的方法,现记录下有update.exe来更新winform.exe。
1.先建一个记录有版本信息的xml文件,update.xml
其中的代码如下:
<root xmlns="">
<module>
<moduleName>BLL.dll</moduleName>
<version>4.0</version>
<appUrl>http://10.77.150.105:7003/webservice/Release/BLL.dll</appUrl>
</module>
<module>
<moduleName>DAL.dll</moduleName>
<version>4.0</version>
<appUrl>http://10.77.150.105:7003/webservice/Release/DAL.dll</appUrl>
</module>
<module>
<moduleName>winform.exe</moduleName>
<version>4.0</version>
<appUrl>http://10.77.150.105:7003/webservice/Release/demo.exe</appUrl>
</module>
<module>
<moduleName>Model.dll</moduleName>
<version>4.0</version>
<appUrl>http://10.77.150.105:7003/webservice/Release/Model.dll</appUrl>
</module>
<module>
<moduleName>RdCard.dll</moduleName>
<version>4.0</version>
<appUrl>http://10.77.150.105:7003/webservice/Release/RdCard.dll</appUrl>
</module>
<module>
<moduleName>sdtapi.dll</moduleName>
<version>4.0</version>
<appUrl>http://10.77.150.105:7003/webservice/Release/sdtapi.dll</appUrl>
</module>
<module>
<moduleName>WltRS.dll</moduleName>
<version>4.0</version>
<appUrl>http://10.77.150.105:7003/webservice/Release/Wltrs.dll</appUrl>
</module>
</root>
update.xml是放在已经部署的webservice文件夹下的。
下面是update.exe代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
namespace Update
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcesses();
foreach (System.Diagnostics.Process p in ps)
{
//MessageBox.Show(p.ProcessName);
if (p.ProcessName.ToLower() == "winform")//停止所有叫"winform"的程序
{
p.Kill();
break;
}
}
string path = Application.StartupPath;
this.VersionCheck(path, "http://10.77.150.105:7003/webservice/myservice.asmx");//webservice地址,我是已经部署到IIS上了,我的更新的版本也在这个目录里面
}
public string GetVer()//获得版本号
{
XmlDocument doc = new XmlDocument();
doc.Load(@"update.xml");
XmlElement root = doc.DocumentElement;
return root.SelectSingleNode("version").InnerText;
}
private void DownloadFile(string source, string fileName)//下载更新文件
{
try
{
System.Net.WebClient myWebClient = new System.Net.WebClient();
myWebClient.DownloadFile(source, fileName);
}
catch (Exception ex)
{
throw new Exception("下载失败,原因是:" + ex.Message, ex);
}
}
private void VersionCheck(string desPath, string webServiceAddress)//版本号校验
{
try
{
#region 查看文件和目录
if (!desPath.EndsWith(@"/"))
desPath += @"/";
if (!System.IO.Directory.Exists(desPath))
{
System.IO.Directory.CreateDirectory(desPath);
}
string tempPath = desPath + @"update/";
if (System.IO.Directory.Exists(tempPath))
{
System.IO.Directory.Delete(tempPath, true);
System.IO.Directory.CreateDirectory(tempPath);
}
else
System.IO.Directory.CreateDirectory(tempPath);
if (!System.IO.File.Exists(desPath + "UpdateConfig.xml"))
{
System.Xml.XmlDocument updateConfig = new System.Xml.XmlDocument();
updateConfig.LoadXml(@"<root></root>");
updateConfig.Save(desPath + "UpdateConfig.xml");
}
#endregion
localhost.MyService appUpdate = new localhost.MyService();
//WebService appUpdate = new WebService();
appUpdate.Url = webServiceAddress;
//System.Xml.XmlDocument serverXmlDoc = ((System.Xml.XmlDocument)appUpdate.AppUpdateVertion());
// System.Xml.XmlDocument serverXmlDoc = appUpdate.AppUpdateVertion();
System.Xml.XmlDocument serverXmlDoc = new XmlDocument();
//serverXmlDoc.LoadXml();
//System.Xml.XmlDocument serverXmlDoc = appUpdate.AppUpdateVertion();
System.Xml.XmlNode xml = appUpdate.AppUpdateVertion();
serverXmlDoc.LoadXml(xml.OuterXml);
System.Xml.XmlDocument localXmlDoc = new System.Xml.XmlDocument();
localXmlDoc.Load(desPath + "UpdateConfig.xml");
bool newVersionExist = false;
bool moduleExist = false;
System.Xml.XmlNode serverNode0 = serverXmlDoc.ChildNodes[0];
System.Xml.XmlNode localNode0 = localXmlDoc.ChildNodes[0];
foreach (System.Xml.XmlNode serverNode in serverNode0)
{
moduleExist = false;
foreach (System.Xml.XmlNode localNode in localNode0)
{
//找到对应模块
if (localNode.ChildNodes[0].InnerText == serverNode.ChildNodes[0].InnerText)
{
moduleExist = true;
//版本号判断
if (localNode.ChildNodes[1].InnerText.CompareTo(serverNode.ChildNodes[1].InnerText) < 0)
{
newVersionExist = true;
DownloadFile(serverNode.ChildNodes[2].InnerText, tempPath + serverNode.ChildNodes[0].InnerText);
}
break;
}
}
//没找到对应模块
if (false == moduleExist)
{
DownloadFile(serverNode.ChildNodes[2].InnerText, tempPath + serverNode.ChildNodes[0].InnerText);
newVersionExist = true;
}
}
//写入新UpdateConfig.xml升级完毕后替换
if (newVersionExist)
{
serverXmlDoc.Save(tempPath + "UpdateConfig.xml");
if (DialogResult.Yes == MessageBox.Show("有新版本,升级否", "提示", MessageBoxButtons.YesNo))
{
string[] dirs = System.IO.Directory.GetFiles(tempPath, "*.*");
string fileName;
foreach (string dir in dirs)
{
fileName = ((dir.Split(Convert.ToChar(@"/")))[dir.Split(Convert.ToChar(@"/")).Length - 1]);
if (System.IO.File.Exists(desPath + fileName))
{
//TODO:可以支持备份以前版本
//System.Diagnostics.Process.Start(Application.StartupPath + @"\demo.exe");
//System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcesses();
System.IO.File.Delete(desPath + fileName);
}
//TODO:如果系统正在运行,您得停止系统,至于如何停止,也许可以使用System.Diagnostics.Process
System.IO.File.Move(dir, desPath + fileName);
}
MessageBox.Show("升级完毕");
}
else
{
//TODO:可以支持重新提示升级
}
}
}
catch (Exception ex)
{
throw new Exception("升级失败,原因是:" + ex.Message, ex);
}
System.Diagnostics.Process.Start("demo.exe");
Close();
Application.Exit();
}
}
}
注:我的版本下载地址是http://10.77.150.105:7003/webservice/Release/winform.exe
update.exe还需要添加引用webservice,webservice上面的代码:
[WebMethod(Description = "读服务器上关于程序各个版本信息的配置文件UpdateConfig.xml")]
public System.Xml.XmlDocument AppUpdateVertion()
{
System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
xml.Load(CurrentPath("/webs1") + @"/UpdateConfig.xml");
return xml;
}
private string CurrentPath(string virtualPath)
{
return HttpContext.Current.Server.MapPath(virtualPath);
}
主要原理是,停止所有的winform.exe,update会建一个文件夹叫update(在程序的Update\bin\Debug目录下),先判断版本号,如果版本号比原来的大,则更新,若不是,则运行原版本的应用程序,若有新的版本,就会,update文件夹内是存放下载的新的版本的winform.exe和其他文件,之后,删除update.exe文件夹的debug文件夹下的相对的文件夹,然后将update文件夹下面的文件移出到debug文件夹下面,最后启动winform.exe。
不懂得可以留言!!!
- winform应用程序自动更新版本
- Winform/WinCE程序版本自动更新
- Winform 自动更新
- winForm自动更新
- 自动更新版本
- Winform 程序自动更新
- C#winform自动更新
- .net winform软件自动更新
- winform c# 自动更新
- C# winform 自动更新
- Winform程序自动更新
- Winform自动更新组件分享
- winform 自动更新 发布IIS
- Android 应用程序自动更新
- 应用程序自动更新源码
- Android应用程序自动更新
- Android 之应用程序自动更新
- C# winform自动更新程序扫盲
- 深入java虚拟机(二)——类的生命周期(上)类的加载和连接
- 深入java虚拟机(三)——类的生命周期(下)类的初始化
- 深入java虚拟机(四)——java虚拟机的垃圾回收机制
- 深入java虚拟机(五)——java本地接口JNI详解
- 深入java虚拟机(六)——类加载的父亲委托机制
- winform应用程序自动更新版本
- 深入java虚拟机(七)深入源码看java类加载器ClassLoader
- 深入java虚拟机(八)开发自己的类加载器
- Java多线程基础知识
- java异常处理机制
- SQL中的事务以及批量更新
- Java多线程总结之线程安全队列Queue
- BlockingQueue详解
- PrepareStatement与Statement之间的区别