Пример #1
0
        //public Boolean GetUpdateNew(Label lblUpdateMsg)
        //{
        //    Boolean flag = false;
        //    try
        //    {
        //        DataTable dt = uhc.GetNewUpdate();
        //        if (dt != null && dt.Rows.Count > 0)
        //        {
        //            for (int i = 0; i < dt.Rows.Count; i++)
        //            {
        //                lblUpdateMsg.Text = "正在获取更新…";
        //                String subFolder = uhc.GetSubFolder(dt.Rows[i]["FileType"].ToString());
        //                String path = Path.Combine(Application.StartupPath, "update\\" + subFolder);
        //                if (!Directory.Exists(path))
        //                {
        //                    Directory.CreateDirectory(path);
        //                }
        //                String file = Path.Combine(path, dt.Rows[i]["FileName"].ToString());
        //                log.WriteLog(file, true);
        //                byte[] bytes = uhc.GetUpdateFileByID(new Guid(dt.Rows[i]["ID"].ToString()));
        //                lblUpdateMsg.Text = "正在保存更新…";
        //                if (bytes != null)
        //                {
        //                    File.WriteAllBytes(file, bytes);
        //                    uhc.SaveUpdateInfo(dt.Rows[i]["ID"].ToString(), dt.Rows[i]["FileName"].ToString(),
        //                        dt.Rows[i]["FileType"].ToString(),
        //                        Convert.ToDateTime(dt.Rows[i]["CreatedServerTime"]).ToString("yyyy-MM-dd HH:mm:ss.fff"));
        //                    flag = true;
        //                }
        //                if (i == dt.Rows.Count - 1)
        //                {
        //                    lblUpdateMsg.Text = "获取更新完毕";
        //                }
        //            }

        //        }
        //        else
        //        {
        //            lblUpdateMsg.Text = "获取更新完毕";
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        log.WriteLog(ex.Message, true);
        //    }
        //    return flag;
        //}


        private void LoadUpdate()
        {
            try
            {
                DataTable dt = uhc.GetUnUsedUpdateInfo();
                if (dt != null && dt.Rows.Count > 0)
                {
                    //UpdateAlert ua = new UpdateAlert();
                    //ua.Show(this);
                    UpdateVisible(true);
                    UpdateProgressInfo("正在应用更新……", 1);
                    Boolean mainFormRun = false;
                    Boolean applyUpdate = true;
                    //1 管理系统文件+不执行,
                    //2 采集系统文件+不执行,
                    //3 管理系统数据+不执行,
                    //4 管理系统文件+数据+执行;
                    //5 采集系统文件+执行
                    //6 管理系统执行
                    //7 采集系统执行
                    //8 管理系统文件+执行
                    //9 电液伺服文件+执行
                    //10 领导版管理+执行
                    String appName = ApplicationHelper.GetApplicationName(updateFlag);
                    mainFormRun = true;
                    if (applyUpdate)
                    {
                        Thread.Sleep(1500);
                        bool bUpdateFlag = true;//更新标记,用来判断是否全部更新
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            Int32  fileState = -1;
                            String subFolder = uhc.GetSubFolder(dt.Rows[i]["FileType"].ToString());
                            String path      = Path.Combine(Application.StartupPath, "update\\" + subFolder);

                            String file      = Path.Combine(path, dt.Rows[i]["FileName"].ToString());
                            String agentPath = Path.Combine(path, "JZUpgradeAgent.exe");
                            if (File.Exists(agentPath))
                            {
                                try
                                {
                                    File.Delete(agentPath);
                                }
                                catch (Exception e)
                                {
                                    logger.Error("删除 JZUpgradeAgent.exe 失败,原因:" + e.ToString());
                                }
                            }

                            if (File.Exists(file))
                            {
                                Boolean flag = uhc.UnZipFile(file);
                                if (flag)
                                {
                                    String[] files = Directory.GetFiles(path);
                                    foreach (String fileName in files)
                                    {
                                        if (dt.Rows[i]["FileType"].ToString() == "1" || dt.Rows[i]["FileType"].ToString() == "2" ||
                                            dt.Rows[i]["FileType"].ToString() == "4" || dt.Rows[i]["FileType"].ToString() == "10")
                                        {
                                            //管理、采集文件系统更新
                                            bUpdateFlag = bUpdateFlag & ProcessUpdateFile(fileName, Application.StartupPath);
                                        }
                                        else if (dt.Rows[i]["FileType"].ToString() == "3")
                                        {
                                            //管理系统本地数据库缓存更新
                                            LoadDBData(fileName);
                                        }
                                    }
                                    if (bUpdateFlag == true)
                                    {
                                        File.Delete(file);
                                        fileState = 1;
                                    }
                                }
                                else
                                {
                                    bUpdateFlag = false;
                                    logger.Error("文件解压失败,文件名:" + file);
                                }
                            }
                            if (i == dt.Rows.Count - 1)
                            {
                                UpdateProgressInfo("应用更新完毕", 100);
                            }
                            else
                            {
                                UpdateProgressInfo("正在应用更新……", (int)((100 / dt.Rows.Count) * (i + 1)));
                            }
                            uhc.FinishUpdate(dt.Rows[i]["ID"].ToString(), fileState);
                        }
                        if (bUpdateFlag == false)
                        {
                            MessageBox.Show("部分文件未更新成功,请查看更新日志手动更新。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                    }
                    if (mainFormRun)
                    {
                        try
                        {
                            ProcessStartInfo Info = new ProcessStartInfo();
                            Info.FileName  = Path.Combine(Application.StartupPath, ApplicationHelper.GetApplicationName(updateFlag) + ".exe");
                            Info.Arguments = String.Concat("\"", Application.ProductName, "\" \"", Application.ExecutablePath, "\"");
                            Process.Start(Info);
                        }
                        catch (Exception exxx)
                        {
                            logger.Error(exxx.Message);
                        }
                    }
                }
                else
                {
                    UpdateProgressInfo("获取更新完毕", 100);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
            }
        }