//这是一个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[];
System.Xml.XmlNode localNode0 = localXmlDoc.ChildNodes[];
foreach(System.Xml.XmlNode serverNode in serverNode0)
{
moduleExist = false;
foreach(System.Xml.XmlNode localNode in localNode0)
{
//找到对应模块
if(localNode.ChildNodes[].InnerText == serverNode.ChildNodes[].InnerText)
{
moduleExist = true;
//版本号判断
if(localNode.ChildNodes[].InnerText.CompareTo(serverNode.ChildNodes[].InnerText) < )
{
newVersionExist = true;
if(System.Configuration.ConfigurationSettings.AppSettings["NetStyle"].ToString()=="internet")
{
DownloadFile(serverNode.ChildNodes[].InnerText,tempPath + serverNode.ChildNodes[].InnerText);
}
else
{
DownloadFile(serverNode.ChildNodes[].InnerText,tempPath + serverNode.ChildNodes[].InnerText);
}
}
break;
}
}
//没找到对应模块
if(false == moduleExist)
{ if(System.Configuration.ConfigurationSettings.AppSettings["NetStyle"].ToString()=="internet")
{
DownloadFile(serverNode.ChildNodes[].InnerText,tempPath + serverNode.ChildNodes[].InnerText);
}
else
{
DownloadFile(serverNode.ChildNodes[].InnerText,tempPath + serverNode.ChildNodes[].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 - ]);
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);
}
}

C#制作在线升级程序的更多相关文章

  1. Encrypting bootloader (程序BIN文件加密及在线升级)

    了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). 在上一个博客随笔,我介 ...

  2. C#做的在线升级小程序

    转自原文C#做的在线升级小程序 日前收到一个小任务,要做一个通用的在线升级程序.更新的内容包括一些dll或exe或.配置文件.升级的大致流程是这样的,从服务器获取一个更新的配置文件,经过核对后如有新的 ...

  3. Encrypted bootloader (程序BIN文件加密及在线升级)

    了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). 在上一个博客随笔,我介 ...

  4. STM32 IAP 在线升级详解(转)

    源:http://blog.csdn.net/yx_l128125/article/details/12992773 (扩展-IAP主要用于产品出厂后应用程序的更新作用,考虑到出厂时要先烧写IAP   ...

  5. PIC32MZ 通过USB在线升级 -- USB CDC bootloader

    了解bootloader 的实现,请加QQ: 1273623966 (验证填 bootloader):欢迎咨询或定制bootloader:我的博客主页www.cnblogs.com/geekygeek ...

  6. 【转载】STM32 IAP 在线升级详解

      (扩展-IAP主要用于产品出厂后应用程序的更新作用,考虑到出厂时要先烧写IAP  再烧写APP应用程序要烧写2次增加工人劳动力基础上写了“STM32 IAP+APP ==>双剑合一”链接稍后 ...

  7. Java程序如何自动在线升级

    有时候我们的程序需要连接服务器检测新版本,如果发现新版本则需要自动下载升级.这种需求在Linux下还好说,但在windows下如何替换正在运行的程序文件呢? 当然有办法,步骤如下: 1. 将我们的程序 ...

  8. 微信小程序在线制作 自己制作微信小程序

    小程序是个什么东西?怎么自己制作微信小程序?微信小程序在线制作难吗?最近老是听这类问题,耳朵都长茧子了. 百牛信息技术bainiu.ltd整理发布于博客园 接下来作为一个技术人员的角度就为大家分析一下 ...

  9. ESA2GJK1DH1K升级篇: 快速的移植升级程序到自己的项目(BootLoader程序制作)

    前言 此代码兼容STM32F103全系列 为避免添加上升级程序造成内存不足,请使用128KB Flash及其以上的型号 这篇文章是为了能够让大家快速移植我的升级模板程序到自己的项目 BootLoade ...

随机推荐

  1. 【转】visual studio 2012进行C语言开发[图文]

    原文网址:http://blog.csdn.net/chengyafei0104/article/details/9826025 现在大家计算机大概都脱离XP了,so,之前蛮多可以用的编译器,可能放在 ...

  2. POJ 1094 Sorting It All Out

    题意:给出m对关于n个字母的小于关系,输出通过这些关系能得到的结论,如果可以排序就输出至少知道第几个关系时就可以知道顺序,从小到大输出顺序:如果产生歧义就输出在第几个关系时出现歧义,如果不能得出准确的 ...

  3. [Everyday Mathematics]20150118

    设 $X$ 是线性空间, $\phi_1,\cdots,\phi_n,\phi$ 是 $X$ 上的线性泛函, 试证: $$\bex \phi\in \span\sed{\phi_1,\cdots,\p ...

  4. 如何进行Monkey Test

    如何进行Monkey Test   目录 一 简介 二 测试准备 三 基本命令格式 四 测试Log获取 五 Monkey命令参数介绍 六 保存monkey log以及手机log到sdcard(新增) ...

  5. 两个数组a[N],b[N],其中A[N]的各个元素值已知,现给b[i]赋值,b[i] = a[0]*a[1]*a[2]…*a[N-1]/a[i];

    转自:http://blog.csdn.net/shandianling/article/details/8785269 问题描述:两个数组a[N],b[N],其中A[N]的各个元素值已知,现给b[i ...

  6. 自定义控件:抽屉SlidingDrawer——wrap_content非全屏

    android:allowSingleTap   指示抽屉是否可以打开/通过手柄上的一个水龙头关闭. android:animateOnClick  表示所述抽屉是否应该打开/与当用户点击手柄动画关闭 ...

  7. 【LeetCode 207】Course Schedule

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  8. e2e 自动化集成测试 架构 实例 WebStorm Node.js Mocha WebDriverIO Selenium Step by step (六) 自动化测试结构小节

    上一篇‘e2e 自动化集成测试 架构 京东 商品搜索 实例 WebStorm Node.js Mocha WebDriverIO Selenium Step by step (五) 如何让窗体记录登录 ...

  9. 感知器Perceptron

    Perceptron: 1.一种基于监督的线性分类器,其特点是:1)模型简单,具有很少的学习参数:2)具有可视性,一条直线即可划分:3)基于人工神经网络的原理. 其结构图为:  2.学习的关键技术: ...

  10. hadoop2.6.0汇总:新增功能最新编译 32位、64位安装、源码包、API下载及部署文档

    相关内容: hadoop2.5.2汇总:新增功能最新编译 32位.64位安装.源码包.API.eclipse插件下载Hadoop2.5 Eclipse插件制作.连接集群视频.及hadoop-eclip ...