DMSSSOUpgrad.cs 7.75 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.Configuration;
using System.IO;
using System.Xml;
using System.Diagnostics;
using System.Threading;
using System.Net;

namespace DMSSSOUpgrade
{
    public partial class DMSSSOUpgrad : Form
    {
       // private string filePath = ConfigurationSettings.AppSettings["FilePath"].ToString();
        public DMSSSOUpgrad()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 传递项目的资源文件路径
        /// </summary>
        /// <param name="_filePath"></param>
        public DMSSSOUpgrad(string _filePath)
        {
            InitializeComponent();
            //filePath = _filePath;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
            Application.Exit();
        }
        /// <summary>
        /// 最小化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMini_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        /// <summary>
        /// 下一步按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNext_Click(object sender, EventArgs e)
        {
            try
            {
                DMSSSOService.SSOServices ss = new DMSSSOUpgrade.DMSSSOService.SSOServices();
                object[] oSysList = ss.getUpdateInfor();
                if (((System.Xml.XmlNode[])(oSysList[0]))[0].Value != ConfigurationSettings.AppSettings["VersionNo"].ToString())
                {
                    lbState.Text = "正在下载更新文件,请稍后...";
                    string[] fileList = ((System.Xml.XmlNode[])(oSysList[1]))[0].Value.Split('|');
                    //更新文件的文件地址 D:\Project\TNT\P-DMS SSO\Code\DMSSSO\DMSSSOAgent\bin\Debug
                    string updateSysFilePath = ConfigurationSettings.AppSettings["ServerPath"].ToString();
                    //滚动条
                    pbDownFile.Maximum = fileList.Length+2;
                    pbDownFile.Minimum = 0;
                    System.Net.WebClient webClient = new System.Net.WebClient(); 
                    for (int i = 0; i < fileList.Length; i++)
                    {
                        if (fileList[i].ToString() != "")
                        {
                            string filePathName = Application.StartupPath + fileList[i].ToString().Replace(" ", "");
                            string updateSysFilePathName=@""+updateSysFilePath+ fileList[i].ToString().Replace(" ","");
                            try
                            {
                                System.Net.WebRequest s = System.Net.WebRequest.Create(updateSysFilePathName);
                                System.Net.WebResponse a = s.GetResponse();
                                s.Abort();
                            }
                            catch
                            {
                                MessageBox.Show("更新失败," + fileList[i].ToString() + "文件不存在!");
                                btnNext.Enabled = false;
                                return;
                            }
                            if (File.Exists(filePathName))
                            {
                                //删除原始的
                                File.Delete(filePathName);

                            }
                            //放入新的文件
                            //File.Copy(updateSysFilePathName,filePathName);
                            try
                            {
                                webClient.DownloadFile(updateSysFilePathName, filePathName);
                            }
                            catch(Exception ex) 
                            {
                                MessageBox.Show(ex.ToString());
                            }
                        }
                        pbDownFile.Value++;

                    }
                }
                else
                {
                    MessageBox.Show("你目前的版本已是最新版本,没有可更新的内容!");
                    btnNext.Enabled = false;
                    return;
                }
                //修改版本号

                //获得DMSSSOAgent 的 config 文件
                string strFileNameAgent = Application.StartupPath + @"\DMSSSOAgent.exe.config";
                if(File.Exists(strFileNameAgent))
                {
                    SaveConfig(((System.Xml.XmlNode[])(oSysList[0]))[0].Value, "VersionNo", strFileNameAgent);
                }
                pbDownFile.Value++;
                //获得DMSSSOUpgrade 的 config 文件
                string strFileName = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
                SaveConfig(((System.Xml.XmlNode[])(oSysList[0]))[0].Value, "VersionNo",strFileName);

                pbDownFile.Value++;
                lbState.Text = "程序已更新完毕...";
                //更新按钮状态
                btnNext.Enabled = false;
                btnFinish.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("更新文件下载失败!" + ex.Message.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
        }
        /// <summary>
        /// 完成按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFinish_Click(object sender, EventArgs e)
        {
            Application.Exit();
            Process.Start("DMSSSOAgent.exe", Application.StartupPath + @"\DMSSSOAgent.exe"); 
        }

        #region Load
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DMSSSOUpgrad_Load(object sender, EventArgs e)
        {
            btnFinish.Enabled = false;
        }
        #endregion

        #region Update Config
        
        /// <summary>
        /// 方法保存修改的设置
        /// </summary>
        /// <param name="ConnenctionString"></param>
        /// <param name="strKey"></param>
        private void SaveConfig(string ConnenctionString, string strKey,string strFileName)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(strFileName);
            //找出名称为“add”的所有元素
            XmlNodeList nodes = doc.GetElementsByTagName("add");
            for (int i = 0; i < nodes.Count; i++)
            {
                //获得将当前元素的key属性
                XmlAttribute att = nodes[i].Attributes["key"];
                //根据元素的第一个属性来判断当前的元素是不是目标元素
                if (att.Value == strKey)
                {
                    //对目标元素中的第二个属性赋值
                    att = nodes[i].Attributes["value"];
                    att.Value = ConnenctionString;
                    break;
                }
            }
            //保存上面的修改
            doc.Save(strFileName);
        }
        #endregion


    }
}