Пример #1
0
        static void Main()
        {
            try
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(AppDomain_UnhandledException);

                LogService.InitializeService(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Start.exe.config"));
                log = LogService.Getlog(typeof(Program));
                log.Fatal("======>=Start up=>=====");

                #region 检查是否有服务器地址
                string url = Config.GetXmlValue(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Start.exe.config"), "RemoteURL");
                if (string.IsNullOrEmpty(url))
                {
                    frm_Set frmSet = new frm_Set();
                    frmSet.ShowDialog();
                }

                url = Config.GetXmlValue(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Start.exe.config"), "RemoteURL");
                if (string.IsNullOrEmpty(url))
                {
                    MessageBox.Show("没有指定服务器地址,不能启动程序!", "信息提示", MessageBoxButtons.OK);
                    return;
                }
                #endregion

                #region 检查是否有更新文件
                using (frm_update set = new frm_update())
                {
                    if (set.ShowDialog() != DialogResult.OK)
                    {
                        return;
                    }
                }
                #endregion

                //System.Diagnostics.Process.Start(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "WR.Pos.exe"));
                Form frm = System.Reflection.Assembly.LoadFrom(Application.StartupPath + "\\WR.Client.UI.dll").CreateInstance("WR.Client.UI.frm_main") as Form;
                if (frm != null)
                {
                    Application.Run(frm);
                    log.Fatal("=========<=Shut down=<============");
                }
                else
                {
                    log.Fatal("not find Assembly");
                }

                Application.DoEvents();
                Application.Exit();
            }
            catch (Exception ex)
            {
                log.Error(ex);
                MessageBox.Show("操作中出现错误!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Пример #2
0
        private bool AppUpdate()
        {
            try
            {
                string pathTmp = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "update");

                //上次更新日期
                string   updateDate = Config.GetXmlValue(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Start.exe.config"), "AppUpdate");
                DateTime date_      = DateTime.MinValue;
                //如果更新日期格式错误,初始最早日期
                if (!DateTime.TryParseExact(updateDate, "yyyy-MM-dd HH:mm:ss",
                                            System.Globalization.CultureInfo.GetCultureInfo("zh-CN"),
                                            System.Globalization.DateTimeStyles.None,
                                            out date_))
                {
                    date_ = DateTime.ParseExact("20130101", "yyyyMMdd",
                                                System.Globalization.CultureInfo.GetCultureInfo("zh-CN"));
                }

                updateDate = date_.ToString("yyyyMMddHHmmss");

                wmService         service = new wmService();
                List <FileEntity> files   = service.GetChannel().GetAppFilesList(updateDate);

                if (files.Count > 0)
                {
                    progressBarX1.Text    = "Upgrade file found......";
                    progressBarX1.Maximum = files.Count;

                    this.Text = "System is upgrading...";
                    this.Update();

                    int    buffersize = 1024;
                    byte[] buffer     = new byte[buffersize];
                    int    count      = 0;

                    //如果有更新文件,建立保存文件的临时目录
                    if (Directory.Exists(pathTmp))
                    {
                        Directory.Delete(pathTmp, true);
                    }

                    Directory.CreateDirectory(pathTmp);
                    //记录本次更新日期
                    DateTime dtMax = DateTime.MinValue;

                    int currfile = 1;
                    foreach (FileEntity file in files)
                    {
                        if (dtMax < file.LastTime)
                        {
                            dtMax = file.LastTime;
                        }

                        progressBarX1.Value = currfile;
                        progressBarX1.Text  = string.Format("upgrading[{0}/({1}/{2})]...", file.FileName, currfile, files.Count);
                        log.Fatal(string.Format("upgrade file[{0}]/Ver.Time[{1:yyyy-MM-dd HH:mm:ss}]", file.FileName, file.LastTime));

                        //下载更新文件
                        Stream stream = service.GetChannel().DownloadFileStream(file.FileName, file.MapPath);

                        FileStream fstream = new FileStream(Path.Combine(pathTmp, file.FileName), FileMode.CreateNew, FileAccess.Write, FileShare.Write);
                        while ((count = stream.Read(buffer, 0, buffersize)) > 0)
                        {
                            fstream.Write(buffer, 0, count);
                        }
                        fstream.Flush();
                        fstream.Close();

                        currfile++;
                    }

                    System.Configuration.Configuration config = Config.GetConfig();
                    config.AppSettings.Settings.Remove("AppUpdate");
                    config.AppSettings.Settings.Add("AppUpdate", dtMax.ToString("yyyy-MM-dd HH:mm:ss"));
                    config.Save();

                    progressBarX1.Text = "Upgrade is complete, Starting......";
                    System.Threading.Thread.Sleep(100);

                    //return true;
                }
            }
            catch (Exception ex)
            {
                log.Fatal("Error in upgrade system:");
                log.Fatal(ex);
                //MessageBox.Show("Error in upgrade system!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                if (ex.Message.Contains("SOAP"))
                {
                    if (DialogResult.Yes == MessageBox.Show("Network configuration may be wrong, whether to re configure?", "Information", MessageBoxButtons.YesNo))
                    {
                        frm_Set frmSet = new frm_Set();
                        if (frmSet.ShowDialog() == DialogResult.OK)
                        {
                            MessageBox.Show("Please run the program again.", "Information");
                        }
                    }
                }
            }

            return(true);
        }