⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 delphi 开发的自动升级源码
💻 CS
字号:
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.Xml;

namespace softupdata
{
    public partial class Form1 : Form
    {
        localhost.Service lhs = new softupdata.localhost.Service();
        public Form1()
        {
            InitializeComponent();

            string nVer = lhs.GetVer();
            if (Application.ProductVersion.CompareTo(nVer) <= 0)
                update();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcesses();
            foreach (System.Diagnostics.Process p in ps)
            {
                if (p.ProcessName.ToLower() == "softupdata.vshost.exe")
                {
                    p.Kill();
                }
            }
            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();
                this.label1.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();
            }
            label1.Text = "更新完成";
            File.Delete(Application.StartupPath + @"\update.xml");
            label1.Text = "正在重新启动应用程序...";
            System.Diagnostics.Process.Start(Application.StartupPath + @"\softupdata.vshost.exe");
            Close();
            Application.Exit();
        }
        void update()
        {           
            this.label2.Text = "正在下载...";
            try
            {
                System.Xml.XmlNode xn = lhs.GetUpdateData();
                //Stream sm = File.Create(Application.StartupPath + @"\update.xml");
                XmlTextWriter xw = new XmlTextWriter(Application.StartupPath + @"\update.xml", Encoding.UTF8);
                xn.WriteTo(xw);
                xn.Clone();
                xw.Close();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            } 
        }
    }
}
           

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -