感谢csdn jekytan 的共享 http://download.csdn.net/detail/jekytan/4242666

本地xml文件

<?xml version="1.0" encoding="utf-8"?>
<AutoUpdater>
<AppName>WinUpdate</AppName>
<ReleaseURL>http://127.0.0.1/webdown/</ReleaseURL>
<ReleaseDate>// ::</ReleaseDate>
<ReleaseVersion>1.0.1.99</ReleaseVersion>
<MinVersion>1.0.1.88</MinVersion>
<UpdateDes>
、 添加打印菜单
、 增加DLL
、增加关于模块
</UpdateDes>
<ApplicationStart>WinUpdate.exe</ApplicationStart>
<ShortcutIcon>ico</ShortcutIcon>
<Releases>
<File name="AboutForm.dll" date="2012/2/21 10:07:31" size="" />
</Releases>
</AutoUpdater>

服务器xml文件

  <?xml version="1.0" encoding="utf-8" ?>
- <AutoUpdater>
<AppName>WinUpdate</AppName>
<ReleaseURL>http://127.0.0.1/webdown/</ReleaseURL>
<ReleaseDate>// ::</ReleaseDate>
<ReleaseVersion>1.0.4.98</ReleaseVersion>
<MinVersion>1.0.1.88</MinVersion>
<UpdateDes>、 添加打印菜单 、 增加DLL 、增加关于模块</UpdateDes>
<ApplicationStart>WinUpdate.exe</ApplicationStart>
<ShortcutIcon>WMS.ico</ShortcutIcon>
- <Releases>
<File name="AboutForm.dll" date="2012/3/25 10:07:31" size="" />
<File name="WinUpdate.exe" date="2012/3/25 10:07:31" size="" />
</Releases>
</AutoUpdater>

服务器文件webdown放到IIS127.0.0.1中的根目录下即可

前台

后台

 string tempPath;
ReleaseList localRelease;
ReleaseList remoteRelease;
ReleaseFile[] diff; bool downloaded; int totalSize; const string RetryText = " 重 试 ";
const string FinishText = " 完 成 "; public UpdateForm()
{
InitializeComponent();
} public UpdateForm(
string tempPath,
ReleaseList localRelease,
ReleaseList remoteRelease
)
{
InitializeComponent();
this.tempPath = tempPath;
this.localRelease = localRelease;
this.remoteRelease = remoteRelease; } private void UpdateForm_Load(object sender, EventArgs e)
{
Init();
} private void Init()
{
label2.Text = "下载进度";
label1.Text = string.Format("当前版本:{0} 最新版本:{1} 发布时间:{2}", localRelease.ReleaseVersion, remoteRelease.ReleaseVersion,
remoteRelease.ReleaseDate);
//升级内容
textBox1.Text = remoteRelease.UpdateDescription; diff = localRelease.GetDifferences(remoteRelease, out totalSize);
if (diff == null)
{
button1.Text = "升级完成!";
return;
} progressBar1.Maximum = totalSize * ;
progressBar1.Step = ; Upgrade(); } Thread trd;
private void Upgrade()
{
trd = new Thread(new ThreadStart(DoUpgrade));
trd.IsBackground = true;
trd.Start();
} private void DoUpgrade()
{
downloaded = false;
progressBar1.Value = ;
foreach (ReleaseFile file in diff)
{
try
{
DownloadTool.DownloadFile(tempPath,
remoteRelease.ReleaseUrl +remoteRelease.ReleaseVersion, file.FileName, progressBar1, label2);
}
catch (Exception ex)
{
AppTool.DeleteTempFolder(tempPath);
MessageBox.Show(file.FileName + "下载失败,请稍后再试");
OptionalUpdate = true; trd.Abort();
return;
} }
try
{
foreach (ReleaseFile file in diff)
{
string dir = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory + file.FileName);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
File.Copy(tempPath + file.FileName, AppDomain.CurrentDomain.BaseDirectory + file.FileName, true);
}
}
catch (Exception ex)
{
AppTool.DeleteTempFolder(tempPath);
MessageBox.Show(ex.Message, "更新失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
OptionalUpdate = true;
trd.Abort();
return;
}
remoteRelease.Save(localRelease.FileName);
downloaded = true;
CreateShortcut();
Application.Exit();
AppTool.Start(localRelease.ApplicationStart);
trd.Abort();
} private bool OptionalUpdate
{
set
{
;
}
} private void CreateShortcut()
{
string fileName = remoteRelease.AppName;
foreach (char invalidChar in Path.GetInvalidFileNameChars())
{
fileName = fileName.Replace(invalidChar, '_');
}
string path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) +
"\\" + fileName + ".lnk";
if (File.Exists(path))
return; } private void UpdateForm_FormClosing(object sender, FormClosingEventArgs e)
{
AppTool.DeleteTempFolder(tempPath);
} private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
AppTool.Start(localRelease.ApplicationStart);
} private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
Upgrade();
}

DownloadTool.cs类

 public  class DownloadTool
{
public static void DownloadFile(string localFolder, string remoteFolder, string fileName)
{
//if (!System.IO.Directory.Exists(localFolder))
// System.IO.Directory.CreateDirectory(localFolder);
string url = remoteFolder+ fileName;
string path = localFolder + fileName;
string dir = Path.GetDirectoryName(path);
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
var wc = new WebClient();
wc.DownloadFile(url, path);
} public static string FormatFileSizeDescription(int bytes)
{
if (bytes > * )
return string.Format("{0}M", Math.Round((double)bytes / ( * ), , MidpointRounding.AwayFromZero));
if (bytes > )
return string.Format("{0}K", Math.Round((double)bytes / , , MidpointRounding.AwayFromZero));
return string.Format("{0}B", bytes);
} public static void DownloadFile(string localFolder, string remoteFolder, string fileName, ProgressBar bar,
Label lblSize)
{
Thread.Sleep();//真正用的时候把此行注释掉,现在是为了模拟进度条 string url = remoteFolder + "/" + fileName;
string path = localFolder+ fileName;
string dir = Path.GetDirectoryName(path);
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir); WebRequest req = WebRequest.Create(url);
WebResponse res = req.GetResponse();
if (res.ContentLength == )
return; long fileLength = res.ContentLength;
string totalSize = FormatFileSizeDescription(bar.Maximum);
using (Stream srm = res.GetResponseStream())
{
var srmReader = new StreamReader(srm);
var bufferbyte = new byte[fileLength];
int allByte = bufferbyte.Length;
int startByte = ;
while (fileLength > )
{
int downByte = srm.Read(bufferbyte, startByte, allByte);
if (downByte == )
{
break;
}
;
startByte += downByte;
allByte -= downByte;
int progress = bar.Value + downByte;
progress = progress > bar.Maximum ? bar.Maximum : progress; //bar.BeginInvoke(new invoke(setbar)); //bar.Value = progress; //lblSize.Text = string.Format("已完成{0}/{1}", FormatFileSizeDescription(progress), totalSize);
//float part = (float)startByte / 1024;
//float total = (float)bufferbyte.Length / 1024;
//int percent = Convert.ToInt32((part / total) * 100);
} var fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(bufferbyte, , bufferbyte.Length);
srm.Close();
srmReader.Close();
fs.Close();
}
} public void setbar()
{ }
} internal class AppTool
{
public static void Start(string appName)
{
Process.Start(appName, "ok");
} internal static void DeleteTempFolder(string folder)
{
try
{
Directory.Delete(folder, true);
}
catch
{
}
}
}

program.cs类

public const string UPDATER_EXE_NAME = "AutoUpdate.exe";
public const string ReleaseConfigFileName = "ReleaseList.xml"; private static ReleaseList localRelease;
private static ReleaseList remoteRelease;
private static string tempPath; /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); //获取本地relealist文件信息
string localXmlPath = string.Format("{0}\\{1}", Application.StartupPath, ReleaseConfigFileName);
localRelease = new ReleaseList(localXmlPath);
tempPath = Path.GetTempPath(); try
{
//下载服务器上的relealist文件
DownloadTool.DownloadFile(tempPath, localRelease.ReleaseUrl, ReleaseConfigFileName);
}
catch (WebException ex)
{
AppTool.DeleteTempFolder(tempPath);
Application.Exit();
AppTool.Start(localRelease.ApplicationStart);
return;
}
catch
{
AppTool.DeleteTempFolder(tempPath);
MessageBox.Show("下载更新文件失败,请检查网络和文件夹权限");
Application.Exit();
return;
} //把relealist.xml文件下载到本地后,从本地读取
remoteRelease = new ReleaseList(tempPath + ReleaseConfigFileName); //比较本机文件和服务器上的XML文件
if (localRelease.Compare(remoteRelease) != )
{
if (CheckProcessing() != DialogResult.OK)
{
AppTool.DeleteTempFolder(tempPath);
Application.Exit();
return;
} UpdateForm form = new UpdateForm(tempPath, localRelease, remoteRelease);
Application.Run(form);
}
else
{
AppTool.DeleteTempFolder(tempPath);
Application.Exit();
AppTool.Start(localRelease.ApplicationStart);
} } /// <summary>
/// 判断现在是否有主项目在运行
/// </summary>
/// <returns></returns>
static DialogResult CheckProcessing()
{
string exeName = localRelease.ApplicationStart.Substring(, localRelease.ApplicationStart.Length - );
if (Process.GetProcessesByName(exeName).Length > )
{
var rs = MessageBox.Show(string.Format("请先退出正在运行的{0}", exeName), "警告", MessageBoxButtons.RetryCancel,
MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button1);
if (rs == DialogResult.Retry)
{
return CheckProcessing();
}
return rs;
}
return DialogResult.OK;
}
}

repleaselist.cs类

 private readonly string fileName;
private string applicationStart; private string appName;
private IList<ReleaseFile> files;
private string minVersion;
private string releaseDate;
private string releaseUrl;
private string releaseVersion;
private string shortcutIcon;
private string updateDes; public ReleaseList()
{
LoadXml(
@"<?xml version=""1.0"" encoding=""utf-8""?>
<AutoUpdater>
<AppName></AppName>
<ReleaseURL></ReleaseURL>
<ReleaseDate></ReleaseDate>
<ReleaseVersion></ReleaseVersion>
<MinVersion></MinVersion>
<UpdateDes></UpdateDes>
<ApplicationStart></ApplicationStart>
<ShortcutIcon></ShortcutIcon>
<Releases>
</Releases>
</AutoUpdater>
");
} public ReleaseList(string filePath)
{
fileName = filePath;
Load(filePath);
appName = GetNodeValue("/AutoUpdater/AppName");
releaseDate = GetNodeValue("/AutoUpdater/ReleaseDate");
releaseUrl = GetNodeValue("/AutoUpdater/ReleaseURL");
releaseVersion = GetNodeValue("/AutoUpdater/ReleaseVersion");
minVersion = GetNodeValue("/AutoUpdater/MinVersion");
updateDes = GetNodeValue("/AutoUpdater/UpdateDes");
applicationStart = GetNodeValue("/AutoUpdater/ApplicationStart");
shortcutIcon = GetNodeValue("/AutoUpdater/ShortcutIcon");
XmlNodeList fileNodes = GetNodeList("/AutoUpdater/Releases");
files = new List<ReleaseFile>();
foreach (XmlNode node in fileNodes)
{
files.Add(new ReleaseFile(node.Attributes[].Value, node.Attributes[].Value,
Convert.ToInt32(node.Attributes[].Value)));
}
} /// <summary>
/// 应用程序名
/// </summary>
public string AppName
{
set
{
appName = value;
SetNodeValue("AutoUpdater/AppName", value);
}
get { return appName; }
} /// <summary>
/// 文件名
/// </summary>
public string FileName
{
get { return fileName; }
} /// <summary>
/// 发布url
/// </summary>
public string ReleaseUrl
{
get { return releaseUrl; }
set
{
releaseUrl = value;
SetNodeValue("AutoUpdater/ReleaseURL", value);
}
} /// <summary>
/// 发布日期
/// </summary>
public string ReleaseDate
{
get { return releaseDate; }
set
{
releaseDate = value;
SetNodeValue("AutoUpdater/ReleaseDate", value);
}
} //版本号
public string ReleaseVersion
{
get { return releaseVersion; }
set
{
releaseVersion = value;
SetNodeValue("AutoUpdater/ReleaseVersion", value);
}
} //最小版本号
public string MinVersion
{
get { return minVersion; }
set
{
minVersion = value;
SetNodeValue("AutoUpdater/MinVersion", value);
}
} //升级内容
public string UpdateDescription
{
get { return updateDes; }
set
{
updateDes = value;
SetNodeValue("AutoUpdater/UpdateDes", value);
}
} //应用程序图标
public string ShortcutIcon
{
get { return shortcutIcon; }
set
{
shortcutIcon = value;
SetNodeValue("AutoUpdater/ShortcutIcon", value);
}
} //启动程序
public string ApplicationStart
{
get { return applicationStart; }
set
{
applicationStart = value;
SetNodeValue("AutoUpdater/ApplicationStart", value);
}
} //升级文件集合
public IList<ReleaseFile> Files
{
get { return files; }
set
{
files = value;
RefreshFileNodes();
}
} //版本号比较
public int Compare(string version)
{
string[] myVersion = releaseVersion.Split('.');
string[] otherVersion = version.Split('.');
int i = ;
foreach (string v in myVersion)
{
int myNumber = int.Parse(v);
int otherNumber = int.Parse(otherVersion[i]);
if (myNumber != otherNumber)
return myNumber - otherNumber;
i++;
}
return ;
} //版本号北京
public int Compare(ReleaseList otherList)
{
if (otherList == null)
throw new ArgumentNullException("otherList");
int diff = Compare(otherList.ReleaseVersion);
if (diff != )
return diff;
return (releaseDate == otherList.ReleaseDate)
?
: (DateTime.Parse(releaseDate) > DateTime.Parse(otherList.ReleaseDate) ? : -);
} /// <summary>
/// 版本号比较,并输出总文件大小
/// </summary>
/// <param name="otherList"></param>
/// <param name="fileSize"></param>
/// <returns></returns>
public ReleaseFile[] GetDifferences(ReleaseList otherList, out int fileSize)
{
fileSize = ;
if (otherList == null || Compare(otherList) == )
return null;
var ht = new Hashtable();
foreach (ReleaseFile file in files)
{
ht.Add(file.FileName, file.ReleaseDate);
}
var diffrences = new List<ReleaseFile>();
foreach (ReleaseFile file in otherList.files)
{
//如果本地的XML文件中不包括服务器上要升级的文件或者服务器的文件的发布日期大于本地XML文件的发布日期,开始升级
if (!ht.ContainsKey(file.FileName) ||
DateTime.Parse(file.ReleaseDate) > DateTime.Parse(ht[file.FileName].ToString()))
{
diffrences.Add(file);
fileSize += file.FileSize;
}
}
return diffrences.ToArray();
} /// <summary>
/// 给定一个节点的xPath表达式并返回一个节点
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
public XmlNode FindNode(string xPath)
{
XmlNode xmlNode = SelectSingleNode(xPath);
return xmlNode;
} /// <summary>
/// 给定一个节点的xPath表达式返回其值
/// </summary>
/// <param name="xPath"></param>
/// <returns></returns>
public string GetNodeValue(string xPath)
{
XmlNode xmlNode = SelectSingleNode(xPath);
return xmlNode.InnerText;
} public void SetNodeValue(string xPath, string value)
{
XmlNode xmlNode = SelectSingleNode(xPath);
xmlNode.InnerXml = value;
} /// <summary>
/// 给定一个节点的表达式返回此节点下的孩子节点列表
/// </summary>
/// <param name="xPath"></param>
/// <returns></returns>
public XmlNodeList GetNodeList(string xPath)
{
XmlNodeList nodeList = SelectSingleNode(xPath).ChildNodes;
return nodeList;
} public void RefreshFileNodes()
{
if (files == null) return;
XmlNode node = SelectSingleNode("AutoUpdater/Releases");
node.RemoveAll();
foreach (ReleaseFile file in files)
{
XmlElement el = CreateElement("File");
XmlAttribute attrName = CreateAttribute("name");
attrName.Value = file.FileName;
XmlAttribute attrDate = CreateAttribute("date");
attrDate.Value = file.ReleaseDate;
XmlAttribute attrSize = CreateAttribute("size");
attrSize.Value = file.FileSize.ToString();
el.Attributes.Append(attrName);
el.Attributes.Append(attrDate);
el.Attributes.Append(attrSize);
node.AppendChild(el);
}
}
} /// <summary>
/// 发布的文件信息
/// </summary>
public class ReleaseFile
{
public ReleaseFile()
{
} /// <summary>
/// 文
/// </summary>
/// <param name="fileName">文件名称</param>
/// <param name="releaseDate">发布日期</param>
/// <param name="fileSize">大小</param>
public ReleaseFile(string fileName, string releaseDate, int fileSize)
{
this.FileName = fileName;
this.ReleaseDate = releaseDate;
this.FileSize = fileSize;
} public string FileName { get; set; } public string ReleaseDate { get; set; } public int FileSize { get; set; }
}

winform 更新服务器程序的更多相关文章

  1. SNF开发平台WinForm之十一-程序打包-SNF快速开发平台3.3-Spring.Net.Framework

    原来我们用的是微软自带的打包工具去打包,但感觉好像也是第三方做的打包并且很是麻烦,还有时不成功报错.那综合考虑就找一个简单实用的打包工具吧,就找到了NSIS这个.具体打包步骤如下: 1.安装NSIS ...

  2. 客户端(winform)更新

    winform更新有两种情况,一种是在线更新在线使用:直接右击项目发布出去就可以更新在线使用了.还有一种更新是不用一直连接网络的模式. 1:C#Winform程序如何发布并自动升级--------ht ...

  3. Linux下select的用法--实现一个简单的回射服务器程序

    1.先看man手册 SYNOPSIS       /* According to POSIX.1-2001 */       #include <sys/select.h>       / ...

  4. zeromq学习记录(二)天气更新服务器使用ZMQ_SUB ZMQ_PUB

    /************************************************************** 技术博客 http://www.cnblogs.com/itdef/   ...

  5. 用Java实现多线程服务器程序

    一.Java中的服务器程序与多线程 在Java之前,没有一种主流编程语言能够提供对高级网络编程的固有支持.在其他语言环境中,实现网络程序往往需要深入依赖于操作平台的网络API的技术中去,而Java提供 ...

  6. Linux服务器程序--大数据量高并发系统设计

         在Linux服务器程序中,让系统能够提供以更少的资源提供更多的并发和响应效率决定了程序设计价值!怎样去实现这个目标,它其实是这么多年以来一直追逐的东西.最开始写代码时候,省去一个条件语句.用 ...

  7. 性能追击:万字长文30+图揭秘8大主流服务器程序线程模型 | Node.js,Apache,Nginx,Netty,Redis,Tomcat,MySQL,Zuul

    本文为<高性能网络编程游记>的第六篇"性能追击:万字长文30+图揭秘8大主流服务器程序线程模型". 最近拍的照片比较少,不知道配什么图好,于是自己画了一个,凑合着用,让 ...

  8. 【干货】WordPress系统级更新,程序升级

    [干货]WordPress系统级更新,程序升级 网站技术日新月异,更新升级是维护工作之一,长时间不升级的程序,就如长时间不维护的建筑物一样,会加速老化.功能逐渐缺失直至无法使用.在使用WordPres ...

  9. IM服务器:编写一个健壮的服务器程序需要考虑哪些问题

    如果是编写一个服务器demo,比较简单,只要会socket编程就能实现一个简单C/S程序,但如果是实现一个健壮可靠的服务器则需要考虑很多问题.下面我们看看需要考虑哪些问题. 一.维持心跳 为何要维持心 ...

随机推荐

  1. C++模板之隐式实例化、显示实例化、隐式调用、显示调用和模板特化详解

    模板的实例化指函数模板(类模板)生成模板函数(模板类)的过程.对于函数模板而言,模板实例化之后,会生成一个真正的函数.而类模板经过实例化之后,只是完成了类的定义,模板类的成员函数需要到调用时才会被初始 ...

  2. VirtualBox下安装ubuntu图文教程以及软件安装

    一. 下载安装VirtualBox 官网下载VirtualBox,目前版本:VirtualBox 5.1.8 for Windows hosts x86/amd64 下载好了安装VirtualBox, ...

  3. SKINTOOL 系统不能正常运行

    1..net安装 2.Microsoft Visual C++ Redistributable Package  运行库

  4. __attribute__((noreturn))的用法

    外文地址:http://www.unixwiz.net/techtips/gnu-c-attributes.html __attribute__ noreturn    表示没有返回值 This at ...

  5. Sense2vec with spaCy and Gensim

    如果你在2015年做过文本分析项目,那么你大概率用的是word2vec模型.Sense2vec是基于word2vec的一个新模型,你可以利用它来获取更详细的.与上下文相关的词向量.本文主要介绍该模型的 ...

  6. VS2008给对话框添加背景颜色

    第一种方法如下: 在对话框OnPaint()函数中添加代码 //改变对话框背景颜色 CRect rect; CPaintDC dc(this); GetClientRect(rect); dc.Fil ...

  7. python读写mysql总结

    一.MySQLdb包的安装 1. 在win环境下,下载MySQL-python-1.2.3,有两种方式: (1) 下载src源码.tar.gz(这个源码需要build编译再安装.egg包(当于.jar ...

  8. Identity Server 4 原理和实战(完结)_建立Angular 客户端

    https://material.angular.io/ 第一部是安装angular cli --prefix=ac:前缀 --routing:默认使用路由 style=scss:样式使用scss - ...

  9. 挨踢职场求生法则-----我在IT职场打滚超过15年了,从小小的程序员做到常务副总

    摘要我在IT职场打滚超过15年了,从小小的程序员做到常务副总.相对于其它行业,IT职场应该算比较光明的了,但也陷阱重重,本文说说我的亲身体会,希望大家能在IT职场上战无不胜! 通用法则 法则1:忍耐是 ...

  10. 设置a 标签打开新窗口新姿势

    设置页面中的所有a标签都打开新窗口 1,在写的时候就加上target="_blank" 2,在页头<head></head>里加上 <base tar ...