예제 #1
0
        //http://www.cnblogs.com/thornfield_he/archive/2009/09/22/1571914.html

        public UpdateBackground()
        {
            IniClass iniC     = new IniClass(Application.StartupPath + @"\Update.ini");
            string   versionC = iniC.IniReadValue("update", "version");

            HOST       = "http://" + iniC.IniReadValue("update", "host") + "/EduApi/download/";
            inifileUrl = HOST + "update.ini";
            log("versionC=" + versionC, true);
            if (versionC == null || versionC.Length == 0)
            {
                return;
            }

            ////////////////////////////////////////////////
            //step1. check ini
            ////////////////////////////////////////////////
            string sDir     = Application.StartupPath;
            string sPath1   = sDir + @"\updateS.ini";
            string versionS = "";

            try
            {
                WebClient wc = new WebClient();
                log("download now..." + inifileUrl);
                wc.DownloadFile(new Uri(inifileUrl), sPath1);//download ini file

                IniClass iniS = new IniClass(sPath1);
                versionS    = iniS.IniReadValue("update", "version");
                zipfile     = iniS.IniReadValue("update", "zipfile");
                zipfileUrl  = HOST + zipfile;
                zipfilePath = sDir + "\\" + zipfile;

                log("versionS=" + versionS + ", zipfile=" + zipfile);

                File.Delete(sPath1);
                if (versionS.CompareTo(versionC) <= 0)
                {
                    log("Exit!");
                    return;
                }
            }
            catch (Exception e1)
            {
                string msg = e1.Message;
                log("download error..." + msg);
#if DEBUG
                MessageBox.Show("获取升级配置文件失败:\r\n" + msg, "Warning!!!");
#endif
                return;
            }


            ////////////////////////////////////////////////
            //step2. download
            ////////////////////////////////////////////////
            {
                WebClient _wc = new WebClient();
                log("download now..." + zipfileUrl);
                _wc.DownloadFile(zipfileUrl, zipfilePath);//download zip file
                log("download over..." + zipfilePath);
            }
            ////////////////////////////////////////////////
            //step3. kill & unzip
            ////////////////////////////////////////////////
            log("kill process of ruehelper.exe now...");
            KillProcess(ApplicationName);
            {
                Thread.Sleep(1000);
                log("unzip now...");
                ZipClass.UnZip(zipfilePath, "", 1, null);
                File.Delete(zipfilePath);
            }

            ////////////////////////////////////////////////
            //step4. replace
            ////////////////////////////////////////////////
            log("set update.version=" + versionS);
            iniC.IniWriteValue("update", "version", versionS);//更新成功后将版本写入配置文件

            ////////////////////////////////////////////////
            //step5. run
            ////////////////////////////////////////////////
            log("restart RueHelper.exe now...");
            Application.Exit();//退出升级程序
            log("Process.Start(" + sDir + "\\" + ApplicationName + ") now...");

            try
            {
                Process.Start(sDir + "\\" + ApplicationName);//打开主程序Main.exe
            }
            catch (Exception e)
            {
                log("Process.Start exception. " + e.Message);
            }
        }