如何实现winform客户端智能更新
来源:互联网 发布:淘宝网店引流软件 编辑:程序博客网 时间:2024/05/24 06:44
1、在服务器上建立虚拟目录appUpdate
2、设置升级配置文件UpdateConfig.xml内容,放到虚拟目录appUpdate下:
格式:moduleName为动态链接库名称,vertion为当前版本号,appUrl为提供下载的地址
例子:
<root>
<module>
<moduleName>Module1.dll</moduleName>
<vertion>1.0.0.1</vertion>
<appUrl>http://localhost/appUpdate/Module1.dll</appUrl>
</module>
<module>
<moduleName>Module2.dll</moduleName>
<vertion>1.0.0.1</vertion>
<appUrl>http://localhost/appUpdate/Module2.dll</appUrl>
</module>
<module>
<moduleName>ZiModule2ReportPrint.dll</moduleName>
<vertion>1.0.0.1</vertion>
<appUrl>http://localhost/appUpdate/ZiModule2ReportPrint.dll</appUrl>
</module>
</root>
3、WebService,部署到appUpdate虚拟目录下:
[WebMethodAttribute]
public System.Xml.XmlDocument AppUpdateVertion()
{
System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
xml.Load(CurrentPath("/appUpdate") + @"/UpdateConfig.xml");
return xml;
}
private string CurrentPath(string virtualPath)
{
return HttpContext.Current.Server.MapPath(virtualPath);
}
3、升级版本判断
/// <summary>
///从webServiceAddress中下载新版本
/// </summary>
/// <param name="desPath">新版本的存放目录</param>
/// <param name="webServiceAddress">webService的网址</param>
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 + @"tempDesPathCache/";
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
AppUpdate.AppUpdate appUpdate = new Test.AppUpdate.AppUpdate();
appUpdate.Url = webServiceAddress;
System.Xml.XmlDocument serverXmlDoc = appUpdate.AppUpdateVertion();
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);
}
}
//写入新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.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);
}
}
4、下载文件:
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);
}
}
5、客户端调用:
string path =Application.StartupPath;
VersionCheck(path,"http://localhost/AppUpdate/AppUpdate.asmx")
2、设置升级配置文件UpdateConfig.xml内容,放到虚拟目录appUpdate下:
格式:moduleName为动态链接库名称,vertion为当前版本号,appUrl为提供下载的地址
例子:
<root>
<module>
<moduleName>Module1.dll</moduleName>
<vertion>1.0.0.1</vertion>
<appUrl>http://localhost/appUpdate/Module1.dll</appUrl>
</module>
<module>
<moduleName>Module2.dll</moduleName>
<vertion>1.0.0.1</vertion>
<appUrl>http://localhost/appUpdate/Module2.dll</appUrl>
</module>
<module>
<moduleName>ZiModule2ReportPrint.dll</moduleName>
<vertion>1.0.0.1</vertion>
<appUrl>http://localhost/appUpdate/ZiModule2ReportPrint.dll</appUrl>
</module>
</root>
3、WebService,部署到appUpdate虚拟目录下:
[WebMethodAttribute]
public System.Xml.XmlDocument AppUpdateVertion()
{
System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
xml.Load(CurrentPath("/appUpdate") + @"/UpdateConfig.xml");
return xml;
}
private string CurrentPath(string virtualPath)
{
return HttpContext.Current.Server.MapPath(virtualPath);
}
3、升级版本判断
/// <summary>
///从webServiceAddress中下载新版本
/// </summary>
/// <param name="desPath">新版本的存放目录</param>
/// <param name="webServiceAddress">webService的网址</param>
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 + @"tempDesPathCache/";
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
AppUpdate.AppUpdate appUpdate = new Test.AppUpdate.AppUpdate();
appUpdate.Url = webServiceAddress;
System.Xml.XmlDocument serverXmlDoc = appUpdate.AppUpdateVertion();
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);
}
}
//写入新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.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);
}
}
4、下载文件:
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);
}
}
5、客户端调用:
string path =Application.StartupPath;
VersionCheck(path,"http://localhost/AppUpdate/AppUpdate.asmx")
- 如何实现winform客户端智能更新
- winform智能客户端
- wpf智能客户端实现
- (转贴)部署和更新智能客户端应用程序
- 传统WINFORM VS 智能客户端 VS Asp.net Vs Ajax
- 智能客户端
- 智能客户端
- 智能客户端
- 智能客户端
- 智能客户端
- 智能客户端
- 智能客户端
- SAP HANA Studio客户端版本如何更新
- WinForm客户端调用 WebService时 如何启用Session
- C#客户端(WinForm)开机自动启动实现
- 实现多svn客户端同步更新
- 在Winform中如何实现ListView排序
- winform如何实现上传文件功能?
- 深入探讨Iterator模式(转载)
- 半缘君
- 迟到的游记
- 把过去的文章重新排版了一下
- 在WinForm中使用Web Services 来实现 软件 自动升级( Auto Update ) (C#)
- 如何实现winform客户端智能更新
- Updater Application Block实践
- C++模板
- 一本好书
- 人类究竟有多么强大?
- 唠叨唠叨
- ASP与ADO RECORDSET对象 的错误
- 一个老程序员的心里话
- [劳谋身算]行走社会100绝杀