1. 更新程序和主程序是分开的,得在做一个exe可执行更新程序。
  2. 主程序在登陆时判断是否需要更新。

    我这边判断方式是直接在配置文件里面设置版本号,然后和服务器上面的版本对比,低于服务器版本就更新程序。

  1.   
    //该文件配置在app.config里面
      <!-- 发布前修改版本号 -->

<?xml version="1.0" encoding="utf-8"?>
<configuration>

<appSettings>

  1. <add key="version" value="1.1.6" />

</appSettings>
</configuration>

  获取配置文件信息,前几章随笔里面有提到。

  

  1.          Version now_v = new Version(strval);//当前版本
  2. Version load_v = new Version(model.version.ToString());//历史版本
  3. if (now_v < load_v && MessageBox.Show("检查到新版本,是否更新?", "Update", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  4. {
  5. //更新(调用更新的exe)返回新版本路径
  6. string fileName = Application.StartupPath + @"\ClientUpdate.exe";
  7. Process p = new Process();
  8. p.StartInfo.UseShellExecute = false;
  9. p.StartInfo.RedirectStandardOutput = true;
  10. p.StartInfo.FileName = fileName;
  11. p.StartInfo.CreateNoWindow = true;
  12. p.StartInfo.Arguments = now_v + " " + load_v + " " + cn.showVersion()+" "+ model.download.ToString();//参数以空格分隔,如果某个参数为空,可以传入””
  13. p.Start();
  14. System.Environment.Exit(System.Environment.ExitCode); //结束主线程
  15. }

  3.接下来是更新程序的代码部分

    

  1. private void Form1_Load(object sender, EventArgs e)
  2. {
  3. try
  4. {
  5. String[] CmdArgs = System.Environment.GetCommandLineArgs();
  6. if (CmdArgs.Length > 1)
  7. {
  8.  
  9. //参数0是它本身的路径
  10. String arg0 = CmdArgs[0].ToString();//程序路径
  11. String arg1 = CmdArgs[1].ToString();//旧版本号
  12. String arg2 = CmdArgs[2].ToString();//新版本号
  13. String arg3 = CmdArgs[3].ToString();//更新版本
  14. String arg4 = CmdArgs[4].ToString();//版本地址
  15.  
  16. //更新
  17. lab_version.Text = arg2;
  18.  
  19. this.Text = arg3 + "更新";
  20. UpdateDownLoad(arg4);//
  21.  
  22. listBox1.Items.Add("更新信息");
  23. foreach (var item in CmdArgs)
  24. {
  25. listBox1.Items.Add(item.ToString());
  26. }
  27.  
  28. label6.Text = "完成更新";
  29. button1.Visible = true;
  30.  
  31. }
  32. }
  33. catch (Exception ex)
  34. {
  35. MessageBox.Show(ex.Message);
  36. }
  37.  
  38. }
  39.  
  40. /// <summary>
  41. /// 更新
  42. /// </summary>
  43. /// <param name="verStr"></param>
  44. private void UpdateDownLoad(string verStr)
  45. {
  46. WebClient wc = new WebClient();
  47. wc.DownloadProgressChanged += wc_DownloadProgressChanged;
  48. wc.DownloadFileAsync(new Uri(verStr), "../"+ZipHelper.zipFileFullName);//要下载文件的路径,下载之后的命名
  49. }
  50.  
  51. void wc_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
  52. {
  53. Action act = () =>
  54. {
  55. this.progressBar1.Value = e.ProgressPercentage;
  56. this.label1.Text = e.ProgressPercentage + "%";
  57.  
  58. };
  59. this.Invoke(act);
  60.  
  61. if (e.ProgressPercentage == 100)
  62. {
  63. //下载完成之后开始覆盖
  64.  
  65. ZipHelper.Unzip();//调用解压的类
  66.  
  67. this.button1.Enabled = true;
  68.  
  69. }
  70. }
  71.  
  72. public delegate void ChangeBarDel(System.Net.DownloadProgressChangedEventArgs e);
  73.  
  74. /// <summary>
  75. /// 完成更新之后再次打开主程序
  76. /// </summary>
  77. /// <param name="sender"></param>
  78. /// <param name="e"></param>
  79. private void button1_Click(object sender, EventArgs e)
  80. {
  81. this.Close();
  82. string fileName = Application.StartupPath + @"\GoldenTax.exe";
  83. Process p = new Process();
  84. p.StartInfo.UseShellExecute = false;
  85. p.StartInfo.RedirectStandardOutput = true;
  86. p.StartInfo.FileName = fileName;
  87. p.StartInfo.CreateNoWindow = true;
  88. p.Start();
  89. System.Environment.Exit(System.Environment.ExitCode); //结束主线程
  90. }
  91. }

  4.解压类

  1. class ZipHelper
  2. {
  3.  
  4. public static string zipFileFullName = "Update.rar";
  5. public static void Unzip()
  6. {
  7. try
  8. {
  9. string _appPath = new DirectoryInfo(Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName).Parent.FullName;
  10.  
  11. string s7z = _appPath + @"\7-Zip\7z.exe";
  12. System.Diagnostics.Process pNew = new System.Diagnostics.Process();
  13. pNew.StartInfo.FileName = s7z;
  14. pNew.StartInfo.Arguments = string.Format(" x \"{0}\\{1}\" -y -o\"{0}\"", _appPath + "/../", zipFileFullName);
  15. //x "1" -y -o"2" 这段7z命令的意思: x是解压的意思 "{0}"的位置写要解压文件路径"{1}"这个1的位置写要解压的文件名 -y是覆盖的意思 -o是要解压的位置
  16. pNew.Start();
  17. //等待完成
  18. pNew.WaitForExit();
  19. //删除压缩包
  20. File.Delete(_appPath + @"/../" + zipFileFullName);
  21. }
  22. catch (Exception ex)
  23. {
  24. MessageBox.Show(ex.Message);
  25. }
  26.  
  27. }
  28.  
  29. }

  

winform程序更新的更多相关文章

  1. WinForm窗体更新程序

    流程介绍: 打包参阅:WinForm程序打包说明    图一    图二    图三   实现步骤: 主程序 1.检测是否连上ftp服务器 1.1 连接不上,不检测. 1.2 连接上,如果有更新进程, ...

  2. windows程序消息机制(Winform界面更新有关)

    windows程序消息机制(Winform界面更新有关) 转自:http://www.cnblogs.com/blosaa/archive/2013/05/31/3109586.html 1. Win ...

  3. winform自动更新程序实现

    一.问题背景 本地程序在实际项目使用过程中,因为可以操作电脑本地的一些信息,并且对于串口.OPC.并口等数据可以方便的进行收发,虽然现在软件行业看着动不动都是互联网啊啥的,大有Web服务就是高大上的感 ...

  4. [搜片神器]winform程序自己如何更新自己的方法代码

    DHT抓取程序开源地址:https://github.com/h31h31/H31DHTDEMO 数据处理程序开源地址:https://github.com/h31h31/H31DHTMgr 国外测试 ...

  5. WinForm关于更新程序的设计思路

    开发WINDOWS应用程序一般都会有一个自动更新的功能,这就需要提供一个单独的更新程序来更新主程序,那么主程序怎么检测是否有更新,以及更新程序怎么去更新主程序呢?下面将分开研究分析. 用VS发布向导发 ...

  6. 用winform程序来了解委托和事件

    一.浅谈委托 如果有个过winform 和webform 程序开发的小伙伴一定有个这样的感觉吧,点击Button直接就执行了那个方法,到此他是怎么实现了的呢,大家有考虑过没有? 回到正题,什么是委托呢 ...

  7. 黄聪:C#Winform程序如何发布并自动升级(图解)

    有不少朋友问到C#Winform程序怎么样配置升级,怎么样打包,怎么样发布的,在这里我解释一下打包和发布关于打包的大家可以看我的文章C# winform程序怎么打包成安装项目(图解)其实打包是打包,发 ...

  8. 【转】C#Winform程序如何发布并自动升级(图解)

    有不少朋友问到C#Winform程序怎么样配置升级,怎么样打包,怎么样发布的,在这里我解释一下打包和发布关于打包的大家可以看我的文章C# winform程序怎么打包成安装项目(图解)其实打包是打包,发 ...

  9. WinForm 程序加管理员权限

    在Vista 和 Windows 7 及更新版本的操作系统,增加了 UAC(用户账户控制) 的安全机制,如果 UAC 被打开,用户即使以管理员权限登录,其应用程序默认情况下也无法对系统目录.系统注册表 ...

随机推荐

  1. python连接redis,redis集群

    python连接redis: import redis r = redis.Redis(host='192.168.50.181',port=6002) r.set('user_phone_14900 ...

  2. 修改UITextView光标高度

    自定义UITextView文字字体时,经常出现光标与字体的高度不匹配,可以通过下面代码修改默认的光标高度, //创建子类重写UITextView方法 - (CGRect)caretRectForPos ...

  3. swift - 封装 GCDTimer 和 NSTimer

    封装的类代码 import UIKit /// 控制定时器的类 class ZDTimerTool: NSObject { /// 定时器 // private var timer: Timer? / ...

  4. [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 ...

  5. Fragment 生命周期 全局变量的声明位置

    public class Fragment_shouye extends Fragment { private List<Zixun_shouye> datas; private TopV ...

  6. Linux的crontab应注意事项

    今天遇到一个问题,困扰了好久,刚开始时以为crontab定时任务配置错误,后经过验证没有错误,然后又怀疑到是不是权限问题呀?将权限跟改为root后,重新配置crontab定时任务,还是不行,真是让人气 ...

  7. 开发中常遇到的Python陷阱和注意点-乾颐堂

    最近使用Python的过程中遇到了一些坑,例如用datetime.datetime.now()这个可变对象作为函数的默认参数,模块循环依赖等等. 在此记录一下,方便以后查询和补充. 避免可变对象作为默 ...

  8. vue cli+axios踩坑记录+拦截器使用,代理跨域proxy(更新)

    16319 1.首先axios不支持vue.use()方式声明使用,看了所有近乎相同的axios文档都没有提到这一点建议方式 在main.js中如下声明使用 import axios from 'ax ...

  9. 【UML】UML类图关系(泛化 、继承、实现、依赖、关联、聚合、组合)

    http://www.cnblogs.com/olvo/archive/2012/05/03/2481014.html 继承.实现.依赖.关联.聚合.组合的联系与区别 分别介绍这几种关系: 继承 指的 ...

  10. "UX"将会是下一个Buzzword?

    “用户体验非常重要”.“没有用户体验就没有产品”.“UX就是一切”.不知道从何时开始,用户体验(UX) 这个名词已经变得如此多见了,但是人们真正的认识.认清了什么是用户体验了吗?设计师们常挂在嘴边的用 ...