WINCE 软件更新功能实现

WINCE设备下的一个主程序,需要更新

Mai.exe // 自身需要运行的主程序

Update.exe    // 用以更新的外持式程序 

思路:

1,写一个外部的.exe外挂式程序(我这名叫Update.exe)放置在主程序(QdPca.exe)运行的目录下

2,在主程序中触发更新或其它外部触发方式,说白了就是启动 Update.exe

3,在Update.exe中 

   判断 如果主程序在运行,则关闭主程序(因为如果主程序没有关闭,会导致下面的文件覆盖失败)

   确保主程序关闭后,从放置最新主程序及主程序所需要文件的目录中复制或剪切所有文件到主程序运行目录上

   覆写成功后,重新启动主程序(QdPca.exe)并关闭自身(Update.exe)


其中使用了一个写好的类,用以获取当前正在运行的程序的进程,

具体使用方法见我的上一篇博客:

http://blog.csdn.net/u010921682/article/details/79469620

更新程序的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Collections;

using Terranova.API;

namespace AppUpdate
{
    public partial class AppUpdate : Form
    {


        public AppUpdate()
        {
            InitializeComponent();

            // 关闭 QdPca主程序
            bool result = ProcessCE.IsRunning(@"\ResidentFlash2\QdPca\QdPca.exe");
            if (result == true)
            {
                // 如果发现主程序在运行,则关闭主程序
                bool ret = ProcessCE.FindAndKill(@"\ResidentFlash2\QdPca\QdPca.exe");
                if (ret == false)
                {
                    // 没有发现主程序
                    // 如果关闭失败,则抛出异常
                }
            }

          
            // 将所有要更新的文件放置到指定目录下
            DirectoryInfo theFolder = new DirectoryInfo("\\ResidentFlash2\\QdPca\\update\\");
            scan(theFolder);

       

            // 启动主程序
            Process p = new Process();
            p.StartInfo.FileName = "\\ResidentFlash2\\QdPca\\QdPca.exe";
            p.Start();


            // 关闭自身
            Process.GetCurrentProcess().Kill();

        }

        private void copy(string sPathName, string dPathName)
        {
            //原路径  
            //拷贝到路径  


            //初始化1  
            FileInfo soufile = new FileInfo(sPathName);
            //初始化2   
            FileInfo dest = new FileInfo(dPathName);

            try
            {
                //如果拷贝的路径有这个文件 就删除  
                if (dest.Exists) dest.Delete();
                //执行拷贝到xx  
                soufile.CopyTo(dPathName, true);

            }
            catch (Exception ex)
            {

                MessageBox.Show("拷贝错误" + ex.ToString());
            } 
        }

        //扫描方法  
        private void scan(FileSystemInfo info)
        {
            if (!info.Exists) 
                return;

            DirectoryInfo dir = info as DirectoryInfo;
            //不是目录  
            if (dir == null) 
                return;
            
            String dPath = "\\ResidentFlash2\\QdPca\\";

            FileSystemInfo[] files = dir.GetFileSystemInfos();

            for (int i = 0; i < files.Length; i++)
            {
                FileInfo file = files[i] as FileInfo;
                //是文件  
                if (file != null)
                {
                    copy(file.FullName, dPath+file.Name);

                }
                else scan(files[i]);
            }



        }//end scan
    }
}

猜你喜欢

转载自blog.csdn.net/u010921682/article/details/79469874