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

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

   
//该文件配置在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程序更新的更多相关文章

  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. PAT L2-005 集合相似度(模拟集合set)

    给定两个整数集合,它们的相似度定义为:Nc/Nt*100%.其中Nc是两个集合都有的不相等整数的个数,Nt是两个集合一共有的不相等整数的个数.你的任务就是计算任意一对给定集合的相似度. 输入格式: 输 ...

  2. Python反转

    1切片 s="svdfbffdbdf" a=s[::-1] 2入栈出栈 入栈之后再出栈正好就是了 3reverse 这个函数是列表的....你要先把str转成list list-& ...

  3. Python threading 单线程 timer重复调用函数

    项目中需要使用定时器,每次都使用构造器函数调用: timer = threading.Timer(timerFlag, upload_position) timer.start() 打印线程后发现,每 ...

  4. discuz回贴通知插件实现-配置邮件服务器

    添加smtp服务器,填写相应的smtp服务器,发信人地址,用户名和密码.   填写发件人地址和收件人地址来测试邮件是否发送成功.

  5. windows文件属性操作 dsofile

    dsofile.dll是com组件,.net程序中引用dsofile.dll文件后,程序集名称会变成“Interop.DSOFile.dll”, com组件需要用regsvr32注册,所以需要注册ds ...

  6. 解决ios手机页面overflow scroll滑动很卡的问题

    在移动端html中经常出现横向/纵向滚动的效果,但是在iPhone中滚动速度很慢,感觉不流畅,有种卡卡的感觉,但是在安卓设备上没有这种感觉; 要解决这个问题很简单: 一行代码搞定 -webkit-ov ...

  7. Codeforces 709C 模拟

    C. Letters Cyclic Shift time limit per test:1 second memory limit per test:256 megabytes input:stand ...

  8. 深入浅出 JMS(三) - ActiveMQ 安全机制

    深入浅出 JMS(三) - ActiveMQ 安全机制 一.认证 认证(Authentication):验证某个实体或者用户是否有权限访问受保护资源. MQ 提供两种插件用于权限认证: (一).Sim ...

  9. classification report 使用

    别人写的,但是还是有些不清晰,我最后补上了 最后一行:第一个0.7=(0.5*1+0*1+1*3)/5  其他类似 support行:在真实数据中y_ture中class 0有一个 class 1有1 ...

  10. Remove duplicates

    https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...