winform程序更新
- 更新程序和主程序是分开的,得在做一个exe可执行更新程序。
- 主程序在登陆时判断是否需要更新。
我这边判断方式是直接在配置文件里面设置版本号,然后和服务器上面的版本对比,低于服务器版本就更新程序。
//该文件配置在app.config里面
<!-- 发布前修改版本号 -->
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
- <add key="version" value="1.1.6" />
</appSettings>
</configuration>
获取配置文件信息,前几章随笔里面有提到。
- Version now_v = new Version(strval);//当前版本
- Version load_v = new Version(model.version.ToString());//历史版本
- if (now_v < load_v && MessageBox.Show("检查到新版本,是否更新?", "Update", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
- {
- //更新(调用更新的exe)返回新版本路径
- string fileName = Application.StartupPath + @"\ClientUpdate.exe";
- Process p = new Process();
- p.StartInfo.UseShellExecute = false;
- p.StartInfo.RedirectStandardOutput = true;
- p.StartInfo.FileName = fileName;
- p.StartInfo.CreateNoWindow = true;
- p.StartInfo.Arguments = now_v + " " + load_v + " " + cn.showVersion()+" "+ model.download.ToString();//参数以空格分隔,如果某个参数为空,可以传入””
- p.Start();
- System.Environment.Exit(System.Environment.ExitCode); //结束主线程
- }
3.接下来是更新程序的代码部分
- private void Form1_Load(object sender, EventArgs e)
- {
- try
- {
- String[] CmdArgs = System.Environment.GetCommandLineArgs();
- if (CmdArgs.Length > 1)
- {
- //参数0是它本身的路径
- String arg0 = CmdArgs[0].ToString();//程序路径
- String arg1 = CmdArgs[1].ToString();//旧版本号
- String arg2 = CmdArgs[2].ToString();//新版本号
- String arg3 = CmdArgs[3].ToString();//更新版本
- String arg4 = CmdArgs[4].ToString();//版本地址
- //更新
- lab_version.Text = arg2;
- this.Text = arg3 + "更新";
- UpdateDownLoad(arg4);//
- listBox1.Items.Add("更新信息");
- foreach (var item in CmdArgs)
- {
- listBox1.Items.Add(item.ToString());
- }
- label6.Text = "完成更新";
- button1.Visible = true;
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- /// <summary>
- /// 更新
- /// </summary>
- /// <param name="verStr"></param>
- private void UpdateDownLoad(string verStr)
- {
- WebClient wc = new WebClient();
- wc.DownloadProgressChanged += wc_DownloadProgressChanged;
- wc.DownloadFileAsync(new Uri(verStr), "../"+ZipHelper.zipFileFullName);//要下载文件的路径,下载之后的命名
- }
- void wc_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
- {
- Action act = () =>
- {
- this.progressBar1.Value = e.ProgressPercentage;
- this.label1.Text = e.ProgressPercentage + "%";
- };
- this.Invoke(act);
- if (e.ProgressPercentage == 100)
- {
- //下载完成之后开始覆盖
- ZipHelper.Unzip();//调用解压的类
- this.button1.Enabled = true;
- }
- }
- public delegate void ChangeBarDel(System.Net.DownloadProgressChangedEventArgs e);
- /// <summary>
- /// 完成更新之后再次打开主程序
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button1_Click(object sender, EventArgs e)
- {
- this.Close();
- string fileName = Application.StartupPath + @"\GoldenTax.exe";
- Process p = new Process();
- p.StartInfo.UseShellExecute = false;
- p.StartInfo.RedirectStandardOutput = true;
- p.StartInfo.FileName = fileName;
- p.StartInfo.CreateNoWindow = true;
- p.Start();
- System.Environment.Exit(System.Environment.ExitCode); //结束主线程
- }
- }
4.解压类
- class ZipHelper
- {
- public static string zipFileFullName = "Update.rar";
- public static void Unzip()
- {
- try
- {
- string _appPath = new DirectoryInfo(Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName).Parent.FullName;
- string s7z = _appPath + @"\7-Zip\7z.exe";
- System.Diagnostics.Process pNew = new System.Diagnostics.Process();
- pNew.StartInfo.FileName = s7z;
- pNew.StartInfo.Arguments = string.Format(" x \"{0}\\{1}\" -y -o\"{0}\"", _appPath + "/../", zipFileFullName);
- //x "1" -y -o"2" 这段7z命令的意思: x是解压的意思 "{0}"的位置写要解压文件路径"{1}"这个1的位置写要解压的文件名 -y是覆盖的意思 -o是要解压的位置
- pNew.Start();
- //等待完成
- pNew.WaitForExit();
- //删除压缩包
- File.Delete(_appPath + @"/../" + zipFileFullName);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- }
winform程序更新的更多相关文章
- WinForm窗体更新程序
流程介绍: 打包参阅:WinForm程序打包说明 图一 图二 图三 实现步骤: 主程序 1.检测是否连上ftp服务器 1.1 连接不上,不检测. 1.2 连接上,如果有更新进程, ...
- windows程序消息机制(Winform界面更新有关)
windows程序消息机制(Winform界面更新有关) 转自:http://www.cnblogs.com/blosaa/archive/2013/05/31/3109586.html 1. Win ...
- winform自动更新程序实现
一.问题背景 本地程序在实际项目使用过程中,因为可以操作电脑本地的一些信息,并且对于串口.OPC.并口等数据可以方便的进行收发,虽然现在软件行业看着动不动都是互联网啊啥的,大有Web服务就是高大上的感 ...
- [搜片神器]winform程序自己如何更新自己的方法代码
DHT抓取程序开源地址:https://github.com/h31h31/H31DHTDEMO 数据处理程序开源地址:https://github.com/h31h31/H31DHTMgr 国外测试 ...
- WinForm关于更新程序的设计思路
开发WINDOWS应用程序一般都会有一个自动更新的功能,这就需要提供一个单独的更新程序来更新主程序,那么主程序怎么检测是否有更新,以及更新程序怎么去更新主程序呢?下面将分开研究分析. 用VS发布向导发 ...
- 用winform程序来了解委托和事件
一.浅谈委托 如果有个过winform 和webform 程序开发的小伙伴一定有个这样的感觉吧,点击Button直接就执行了那个方法,到此他是怎么实现了的呢,大家有考虑过没有? 回到正题,什么是委托呢 ...
- 黄聪:C#Winform程序如何发布并自动升级(图解)
有不少朋友问到C#Winform程序怎么样配置升级,怎么样打包,怎么样发布的,在这里我解释一下打包和发布关于打包的大家可以看我的文章C# winform程序怎么打包成安装项目(图解)其实打包是打包,发 ...
- 【转】C#Winform程序如何发布并自动升级(图解)
有不少朋友问到C#Winform程序怎么样配置升级,怎么样打包,怎么样发布的,在这里我解释一下打包和发布关于打包的大家可以看我的文章C# winform程序怎么打包成安装项目(图解)其实打包是打包,发 ...
- WinForm 程序加管理员权限
在Vista 和 Windows 7 及更新版本的操作系统,增加了 UAC(用户账户控制) 的安全机制,如果 UAC 被打开,用户即使以管理员权限登录,其应用程序默认情况下也无法对系统目录.系统注册表 ...
随机推荐
- python连接redis,redis集群
python连接redis: import redis r = redis.Redis(host='192.168.50.181',port=6002) r.set('user_phone_14900 ...
- 修改UITextView光标高度
自定义UITextView文字字体时,经常出现光标与字体的高度不匹配,可以通过下面代码修改默认的光标高度, //创建子类重写UITextView方法 - (CGRect)caretRectForPos ...
- swift - 封装 GCDTimer 和 NSTimer
封装的类代码 import UIKit /// 控制定时器的类 class ZDTimerTool: NSObject { /// 定时器 // private var timer: Timer? / ...
- [leetcode]340. Longest Substring with At Most K Distinct Characters至多包含K种字符的最长子串
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
- Fragment 生命周期 全局变量的声明位置
public class Fragment_shouye extends Fragment { private List<Zixun_shouye> datas; private TopV ...
- Linux的crontab应注意事项
今天遇到一个问题,困扰了好久,刚开始时以为crontab定时任务配置错误,后经过验证没有错误,然后又怀疑到是不是权限问题呀?将权限跟改为root后,重新配置crontab定时任务,还是不行,真是让人气 ...
- 开发中常遇到的Python陷阱和注意点-乾颐堂
最近使用Python的过程中遇到了一些坑,例如用datetime.datetime.now()这个可变对象作为函数的默认参数,模块循环依赖等等. 在此记录一下,方便以后查询和补充. 避免可变对象作为默 ...
- vue cli+axios踩坑记录+拦截器使用,代理跨域proxy(更新)
16319 1.首先axios不支持vue.use()方式声明使用,看了所有近乎相同的axios文档都没有提到这一点建议方式 在main.js中如下声明使用 import axios from 'ax ...
- 【UML】UML类图关系(泛化 、继承、实现、依赖、关联、聚合、组合)
http://www.cnblogs.com/olvo/archive/2012/05/03/2481014.html 继承.实现.依赖.关联.聚合.组合的联系与区别 分别介绍这几种关系: 继承 指的 ...
- "UX"将会是下一个Buzzword?
“用户体验非常重要”.“没有用户体验就没有产品”.“UX就是一切”.不知道从何时开始,用户体验(UX) 这个名词已经变得如此多见了,但是人们真正的认识.认清了什么是用户体验了吗?设计师们常挂在嘴边的用 ...