Newer
Older
GHFX_REFACTOR / Program.cs
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;

namespace Cyberpipe
{
    static class Program
    {
        static string ent = "C:\\CyberPipe";
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            ValidateConfig.LoadConfig();
            string localConfigPath = Application.StartupPath + "/Config.xml";

            //获取程序及版本信息
            string currentVersionCode = Assembly.GetExecutingAssembly().GetName().Version.ToString();
            //获取文件版本信息
            FileVersionInfo.GetVersionInfo(Application.ExecutablePath);
            Utility.SysInfoConfig();
            string configUrl = getProgramUrl(localConfigPath, "configurl");
            if (configUrl != "")
            {
                String newVersionCode = getVersionCode(configUrl, currentVersionCode);
                if (newVersionCode != currentVersionCode)
                {
                    if (MessageBox.Show("检测到新版本,是否立即更新?", "智慧管网太湖新城规划分析系统", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {

                        remoteUpDate();

                        string programUrl = getProgramUrl(localConfigPath, "programurl");
                        if (programUrl != "")
                        {
                            Process.Start(ent + "/UpDateProgress.exe").WaitForExit();
                            return;
                        }

                    }
                }
            }
            
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Utility.SetParams();

            if (Utility.isNeedLogin)//是否需要登录功能
            {
                FrmLogin frm = new FrmLogin();
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    if (Utility.userRole != null)
                    {
                        Application.Run(new MainFrm());
                    }
                    else
                    {
                        MessageBox.Show("该用户没有分配任何角色!");
                    }
                }
            }
            else
            {
                Application.Run(new MainFrm());
            }

        }

        private static void remoteUpDate()
        {
            if (!File.Exists(ent))
            {
                Directory.CreateDirectory(ent);
            }
            string filePath_exe = ent + "\\UpDateProgress.exe";
            string filePath_exeConfig = ent + "\\UpDateProgress.exe.config";
            string filePath_exePdb = ent + "\\UpDateProgress.pdb";
            string filePath_config = ent + "\\Config.xml";

            string localPath_exe = Application.StartupPath + "\\UpDateProgress.exe";
            string localPath_exeConfig = Application.StartupPath + "\\UpDateProgress.exe.config";
            string localPath_exePdb = Application.StartupPath + "\\UpDateProgress.pdb";
            string localPath_config = Application.StartupPath + "\\Config.xml";

            //TODO LIST:判断文件是否存在,存在就删除
            //delOldVersion(filePath_exe);
            File.Copy(localPath_exe, filePath_exe, true);
            File.Copy(localPath_exeConfig, filePath_exeConfig, true);
            File.Copy(localPath_exePdb, filePath_exePdb, true);
            File.Copy(localPath_config, filePath_config, true);
        }

        private static void createFolder(String folerName)
        {
            if (!Directory.Exists(folerName))
            {
                Directory.CreateDirectory(folerName);
            }
        }

        private static void delOldVersion(String fileName)
        {
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
        }

        //获取新版本程序的路径
        private static string getProgramUrl(string fileName, string nodeName)
        {
            string url = "";
            try
            {
                XmlTextReader xmlReader = new XmlTextReader(fileName);
                while (xmlReader.Read())
                {
                    if (xmlReader.Name == nodeName)
                    {
                        url = xmlReader.ReadElementContentAsString();
                        break;
                    }
                }
                xmlReader.Close();
            }
            catch (Exception e)
            {
                url = "";
            }
            return url;
        }

        //获取版本号
        private static string getVersionCode(string fileName, string currentVersionCode)
        {
            string versionCode="";
            try
            {
                XmlTextReader xmlReader = new XmlTextReader(fileName);
                while (xmlReader.Read())
                {
                    if (xmlReader.Name == "versioncode")
                    {
                        versionCode = xmlReader.ReadElementContentAsString();
                        break;
                    }
                }
                xmlReader.Close();

            }
            catch (Exception e)
            {
                versionCode = currentVersionCode;
                return versionCode;
            }
            return versionCode;
        }

    }
}