FrmUpdate.cs 2.2 KB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace App
{
    public partial class FrmUpdate : Form
    {
        public FrmUpdate()
        {
            InitializeComponent();
        }

        private void FrmUpdate_Load(object sender, EventArgs e)
        {
            System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcesses();
            foreach (System.Diagnostics.Process p in ps)
            {
                //MessageBox.Show(p.ProcessName);
                if (p.ProcessName.ToLower() == "customerapplication")
                {
                    p.Kill();
                    break;
                }
            }
            XmlDocument doc = new XmlDocument();
            doc.Load(Application.StartupPath + @"\update.xml");
            XmlElement root = doc.DocumentElement;
            XmlNode updateNode = root.SelectSingleNode("filelist");
            string path = updateNode.Attributes["sourcepath"].Value;
            int count = int.Parse(updateNode.Attributes["count"].Value);
            for (int i = 0; i < count; i++)
            {
                XmlNode itemNode = updateNode.ChildNodes[i];
                string fileName = itemNode.Attributes["name"].Value;
                FileInfo fi = new FileInfo(fileName);
                fi.Delete();
                //File.Delete(Application.StartupPath + @"\" + fileName);
                this.LabProc.Text = "正在更新: " + fileName + " (" + itemNode.Attributes["size"].Value + ") ...";
                FileStream fs = File.Open(fileName, FileMode.Create, FileAccess.Write);
                fs.Write(System.Convert.FromBase64String(itemNode.SelectSingleNode("value").InnerText), 0, int.Parse(itemNode.Attributes["size"].Value));
                fs.Close();
            }
            LabProc.Text = "更新完成";
            File.Delete(Application.StartupPath + @"\update.xml");
            LabProc.Text = "正在重新启动应用程序...";
            System.Diagnostics.Process.Start("CustomerApplication.exe");
            Close();
            Application.Exit();

        }
    }
}