//这是一个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. java web 学习四(http协议)

    一.什么是HTTP协议 HTTP是hypertext transfer protocol(超文本传输协议)的简写,它是TCP/IP协议的一个应用层协议,用于定义WEB浏览器与WEB服务器之间交换数据的 ...

  2. Dubbo入门实例--转载

    原文地址:http://blog.csdn.net/ruishenh/article/details/23180707?utm_source=tuicool 1.   概述 Dubbo是一个分布式服务 ...

  3. Hibernate逆向工程

    MySQL Administrator 创建表   MyEclipse Database Explorer视图: 1. New  2 .Driver template: MySQL Connector ...

  4. android string[] arraylist<string>互转

    string[] to arraylist<string>:eg:string[] str; arraylist<string> arr; for(int i=0;i<s ...

  5. Running a Remote Desktop on a Windows Azure Linux VM (远程桌面到Windows Azure Linux )-摘自网络(试了,没成功 - -!)

                              A complete click-by-click, step-by-step video of this article is available ...

  6. homework_01

    一. 程序的架构和思路: 这段求解最大子数组之和的程序使用的主要思想是贪心算法,即每一步求出的都是当前的最优解. 首先这道题要分两种情况来讨论: 1)如果当前的输入中所有的数均为负数时,那么最后的解就 ...

  7. Windows下cmd的替代软件——PowerCmd

    Powercmd 是一款运行在windows下的cmd增强软件(A Better Command Prompt Replacement Tool),当前最新的版本为2.2. 官方提供试用版,貌似没有功 ...

  8. iOS中的谓词(NSPredicate)使用

    http://www.cocoachina.com/ios/20160111/14926.html 首先,我们需要知道何谓谓词,让我们看看官方的解释: The NSPredicate class is ...

  9. HDU 4950 Monster (水题)

    Monster 题目链接: http://acm.hust.edu.cn/vjudge/contest/123554#problem/I Description Teacher Mai has a k ...

  10. POJ 1789 Truck History (最小生成树)

    Truck History 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/E Description Advanced Carg ...