C#下实现在线升级

//这是一个webservice

private AppUpdate.UpdateServ UpdateSvr;


private void button1_Click(object sender, System.EventArgs e)
{
 
if(LinkWebServices()==true)
{
this.label1.Text="连接服务器. PASS";

if(CheckVer()==true)
{
this.label2.Text="检查最新版本并下载.PASS";
 
}

else
{
this.label2.Text="检查最新版本并下载.FAIL";
}

}

else
{
this.label1.Text="连接服务器.FAIL";
}

}


//这是用来与升级服务器建立连接
private bool LinkWebServices()
{
try
{
UpdateSvr
=new UpdateServ();
return true;
}

catch
{
return false;
}

}


//调用webservice用来检查是不是有最新的版本
private bool CheckVer()
{
string path =Application.StartupPath;
try
{
VersionCheck(path);
return true;
}

catch(Exception ex)
{
MessageBox.Show(ex.ToString());
return false;
}

}


private void VersionCheck(string desPath)
{
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



System.Xml.XmlDocument serverXmlDoc 
= UpdateSvr.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;
if(System.Configuration.ConfigurationSettings.AppSettings["NetStyle"].ToString()=="internet")
{
DownloadFile(serverNode.ChildNodes[
2].InnerText,tempPath + serverNode.ChildNodes[0].InnerText);
}

else
{
DownloadFile(serverNode.ChildNodes[
3].InnerText,tempPath + serverNode.ChildNodes[0].InnerText);
}

}

break;
}

}

//没找到对应模块
if(false == moduleExist)
{

if(System.Configuration.ConfigurationSettings.AppSettings["NetStyle"].ToString()=="internet")
{
DownloadFile(serverNode.ChildNodes[
2].InnerText,tempPath + serverNode.ChildNodes[0].InnerText);
}

else
{
DownloadFile(serverNode.ChildNodes[
3].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);
}

}


//下载最新的文件

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);
}

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/407786.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

客户端版本和服务器版本上传软件

FileZilla是一个免费开源的FTP软件,分为客户端版本和服务器版本,具备所有的FTP软件功能。可控性、有条理的界面和管理多站点的简化方式使得Filezilla客户端版成为一个方便高效的FTP客户端工具,而FileZilla Server则是一个小巧并且可靠的支持F…

C语言的Trim, LTrim, RTrim

抽空写了个C语言的Trim(), LTrim(), RTrim()&#xff0c;代码如下&#xff1a; #include <iostream>using namespace std;char * LTrim(char * line) // 去除字符串最前面的空格符 {while(isspace(*line))line;return line;}char * RTrim(char * line) // 去除…

一个Ext2+SWFUpload做的图片上传对话框

一个Ext2SWFUpload做的图片上传对话框的例程我们先看看对话框的布局&#xff1a; 布局就是在一个窗口里内嵌一个表格控件&#xff0c;窗口的底部工具条带一个进度条&#xff0c;表格的顶部工具条带几个操作按钮和一个下来选择框&#xff0c;底部工具条作为一个信息显示区域显示…

【STL基础】list

list 构造函数&#xff1a; //default: list<T> l; //空的list//fill: list<T> l(n); //n个元素, 元素默认初始化 list<T> l(n, value); //n个元素值为value//range: list<T> l(first, last); //两个迭代器之间的…

带CheckBox的CListCtrl,源码可下载

实现了一个带CheckBox的CListCtrl&#xff0c;源码可从http://d.download.csdn.net/down/2804276/JoeBlackzqq下载。

grid比flex更强大的属性

dispaly:grid;网格布局 学习博客地址 http://www.ruanyifeng.com/blog/2019/03/grid-layout-tutorial.html http://www.ruanyifeng.com/blog/2020/08/five-css-layouts-in-one-line.html

单交换机VLAN 配置和结果验证(51cto-o8)

1. 实验线路连接图使用Cisco Packet Tracer5.3 构建拓扑结构图 2. 实验内容(1) 按图配置各台计算机IP 地址。(2) 参阅教材中内容&#xff0c;完成单交换机上的VLAN 配置&#xff0c;配置要求如图 所示&#xff0c;使用show vlan 指令查看VLAN的配置情况&#xff0c;并使用Ping …

Dottext.Web.UI.Handlers.BlogExistingPageHandler

dottext 转2005时的一个错误 在建立Dottext.Web.UI.Handlers.BlogExistingPageHandler对象时 抱不存在的错误 解决办法,把这个类直接复制到 项目 Dottext.Common 里去 web.config 再添加 type"Dottext.Web.UI.Handlers.BlogExistingPageHandler,Dottext.Common" 就可…

Linux下计算程序运行时间的两种方法

Linux下计算程序运行时间的两种方法 1.以下是我在网上看到的&#xff1a; 有时候我们要计算程序执行的时间.比如我们要对算法进行时间分析 ..这个时候可以使用下面这个函数. #include <sys/time.h> int gettimeofday(struct timeval *tv,struct timezone *tz); stru…

vue路由懒加载和组件懒加载

vue路由懒加载和组件懒加载其实原理一样 常用的懒加载方式有两种&#xff1a;即使用vue异步组件 和 ES中的import 路由懒加载 ES 提出的import方法&#xff0c; 方法如下&#xff1a;const HelloWorld &#xff08;&#xff09;>import(需要加载的模块地址) &#xff08;不…

UML:类图复习-鸡生蛋,蛋生鸡

这是前一阵《高级软件工程》课堂上&#xff0c;老师随堂出的一道讨论题&#xff0c;随手贴在这里&#xff1a; ps: 今天是520&#xff0c;正好聊一些OoXx&#xff0c;关于爱的扯淡话题&#xff1a;&#xff09; 题目&#xff1a;“鸡生蛋&#xff0c;蛋孵鸡”&#xff0c;世间万…

[转] 硬盘工具DiskMan使用图解

① 图解DM硬盘分区的基本使用(图) 对于一个新硬盘来说&#xff0c;首先必须进行的工作就是进行分区&#xff0c;只有这样才能正常使用&#xff0c;同时分区也是为方便我们进行资料的管理。DOS中的Fdisk是一个很小巧的工具&#xff0c;但是在使用上有些麻烦&#xff0c;特别是…

Linux下检测网络状态是否正常

// Linux下检测网络状态是否正常#include <sys/types.h>#include <string.h>#include <stdlib.h>#include <sys/ioctl.h>#include <stdio.h>#include <errno.h>#include <net/if.h>struct ethtool_value {__uint32_t cmd;__uin…

websocket阮一峰博客地址教学

http://www.ruanyifeng.com/blog/2017/05/websocket.html WebSocket 教程

asp.net中MaintainScrollPositionOnPostback属性的使用

可能我们会经常遇到这种情况&#xff0c;当页面内容比较多的时候&#xff0c;当用户执行操作执行一次页面回送后&#xff0c;页面又重新从顶端开始显示&#xff0c;用户不得不重新拖动滚动条回到先前的位置&#xff0c;这会给用户带来很不友好的体验。即时使用updatepanel也会有…

Linux下显示当前目录下的全部目录或文件

Linux终端中显示当前目录下的所有目录和文件&#xff08;不包含隐藏文件&#xff09;&#xff1a; [rootlocalhost ~]# ll     // 显示所有目录和文件总用量 124-rw------- 1 root root 2382 5月 6 07:28 anaconda-ks.cfgdrwxr-xr-x 2 root root 4096 6月 25 16:45 …