程序-客户端自动更新

程序-客户端自动更新


文章转自: http://bbs.bccn.net/viewthread.php?tid=428255&extra=page%3D1%26amp%3Bfilter%3Ddigest&page=1
作者:wangnannan

这是自动更新是单独的一个exe 可能有童鞋要问 为啥子是单独一个exe呢 俺的思路是这样 这个exe根据版本号从服务端下载程序 下载完成后 替换主程序的exe 并重新启动主程序exe
看看这个autoupdate的代码吧
主要如下 这里提供一个思路

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Update;
using System.Threading;
using System.IO;
using System.Runtime.InteropServices;
namespace autoUpdate
{
public partial class Form1 : Form
{
[DllImport(“zipfile.dll”)]
public static extern int MyZip_ExtractFileAll(string zipfile, string pathname);
public Form1()
{
InitializeComponent();

        //清除之前下载来的rar文件
        if (File.Exists(Application.StartupPath + "\\Update_autoUpdate.rar"))
        {
            try
            {
                File.Delete(Application.StartupPath + "\\Update_autoUpdate.rar");
            }
            catch (Exception)
            {


            }

        }
        if (Directory.Exists(Application.StartupPath + "\\autoupload"))
        {
            try
            {
                Directory.Delete(Application.StartupPath + "\\autoupload", true);
            }
            catch (Exception)
            {


            }
        }
       
        //检查服务端是否有新版本程序
        checkUpdate();
        timer1.Enabled = true;
    }

    SoftUpdate app = new SoftUpdate(Application.ExecutablePath, "xinDianClient");
    public void checkUpdate()
    {

        app.UpdateFinish += new UpdateState(app_UpdateFinish);
        try
        {
            if (app.IsUpdate)
            {
                app.Update();
            }
            else
            {
                MessageBox.Show("未检测到新版本!");
                Application.Exit();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    void app_UpdateFinish()
    {
        //解压下载后的文件
        string path = app.FinalZipName;
        if (File.Exists(path))
        {
             //后改的 先解压滤波zip植入ini然后再重新压缩
            string dirEcgPath = Application.StartupPath + "\\" + "autoupload";
            if (!Directory.Exists(dirEcgPath))
            {
                Directory.CreateDirectory(dirEcgPath);
            }
            //开始解压压缩包
            MyZip_ExtractFileAll(path, dirEcgPath);

            try
            {
                //复制新文件替换旧文件
                DirectoryInfo TheFolder = new DirectoryInfo(dirEcgPath);
                foreach (FileInfo NextFile in TheFolder.GetFiles())
                {
                    File.Copy(NextFile.FullName, Application.StartupPath + "\\program\\" + NextFile.Name, true);
                }
                Directory.Delete(dirEcgPath,true);
                File.Delete(path);
                //覆盖完成 重新启动程序
                path = Application.StartupPath + "\\program";
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                process.StartInfo.FileName = "xinDianClient.exe";
                process.StartInfo.WorkingDirectory = path;//要掉用得exe路径例如:"C:\windows";               
                process.StartInfo.CreateNoWindow = true;
                process.Start();

                Application.Exit();
            }
            catch (Exception)
            {
                MessageBox.Show("请关闭系统在执行更新操作!");
                Application.Exit();
            }

           
        
           
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        label2.Text = "下载文件进度:" + COMMON.CommonMethod.autostep.ToString() + "%";
        if (COMMON.CommonMethod.autostep==100)
        {
            timer1.Enabled = false;
        }
    }


}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值