Exemplo n.º 1
0
        private void Thread_CheckVer()
        {
            if (ApplicationRunPath == string.Empty)
            {
#if DEBUG
                MessageBox.Show("服务器更新 路径为空");
#endif
                Console.WriteLine("服务器更新 路径为空");
                return;
            }
            int Interval = 60; //时间间隔,单位秒
            Interval *= 10;    //分钟
            Interval *= 1000;  //毫秒
            while (true)
            {
                try
                {
                    if (!Ping(RemoteHttp_IPAddr))
                    {
                        Thread.Sleep(Interval);
                        continue;
                    }
                    Dictionary <string, string> Path;
                    if (CheckVersion(out Path))
                    {
                        if (MessageBox.Show("服务器有最新版本是否下载?", MsgTitle, MessageBoxButton.OKCancel) != MessageBoxResult.OK)
                        {
                            Environment.Exit(0);
                        }
                        else
                        {
                            this.Dispatcher.BeginInvoke(new Action(() =>
                            {
                                txt_FileAmt.Text = Path.Count.ToString("00");
                                pb_Total.Maximum = Path.Count;
                                pb_Total.Value   = 0;
                                pb_Current.Value = 0;
                            }));
                            MyFunction.AppendUpdate_LOG(string.Format("{0}\t需要下载", DateTime.Now.ToString("HH:mm:ss:fff")));
                            //开始下载
                            Download(Path, ApplicationRunPath);
                        }
                    }
                    else
                    {
                        MyFunction.AppendUpdate_LOG(string.Format("{0}\t不需要下载", DateTime.Now.ToString("HH:mm:ss:fff")));
                        Console.WriteLine(string.Format("{0}\t不需要下载", DateTime.Now.ToString("HH:mm:ss:fff")));
                        continue;
                    }
                }
                catch (Exception ex)
                {
                    MyFunction.AppendUpdate_LOG("升级模块异常:" + ex.Message + "\r\n" + ex.StackTrace);
                }
                finally
                {
                    Thread.Sleep(Interval);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 检查版本是否为最新版本
        /// </summary>
        /// <param name="DownloadPath_Name">下载文件路径 文件名</param>
        /// <returns></returns>
        public bool CheckVersion(out Dictionary <string, string> DownloadPath_Name)
        {
            bool NewVersion = false;

            DownloadPath_Name = new Dictionary <string, string>();
            try
            {
                OpXML xml = new OpXML();
                list_XN = OpXML.GetChildNodes(IIS_Url, _Version);
                foreach (XmlNode node in list_XN)
                {
                    this.Server_Ver = GetVerInt(node.Value);
                    Console.WriteLine(string.Format("{0}\t服务器版本:{1}\t已经下载版本:{2}\t当前版本:{3}",
                                                    DateTime.Now.ToString("HH:mm:ss:fff"),
                                                    this.Server_Ver,
                                                    this.Download_Ver,
                                                    this.Current_Ver));
                    MyFunction.AppendUpdate_LOG(string.Format("{0}\t服务器版本:{1}\t已经下载版本:{2}\t当前版本:{3}",
                                                              DateTime.Now.ToString("HH:mm:ss:fff"),
                                                              this.Server_Ver,
                                                              this.Download_Ver,
                                                              this.Current_Ver));
                }
                list_XN = OpXML.GetChildNodes(IIS_Url, _Update);
                foreach (XmlNode node in list_XN)
                {
                    if (node.Name != _File)
                    {
                        continue;
                    }
                    string webpath, filename;
                    if (GetUpdateInf(node, out webpath, out filename))
                    {
                        DownloadPath_Name.Add(webpath, filename);
                    }
                }
            }
            catch (Exception ex)
            {
#if DEBUG
                MessageBoxX.Show(string.Format("检查版本:{0}", ex.Message), MsgTitle);
#endif
                Console.WriteLine(string.Format("{1} 检查版本:{0}", ex.Message, MsgTitle));
                MyFunction.AppendUpdate_LOG(string.Format("{1} 检查版本:{0}", ex.Message, MsgTitle));
            }
            //以网络的版本比对当前运行的版本,及下载的版本
            if (this.Server_Ver != this.Current_Ver && this.Server_Ver != this.Download_Ver)
            {
                NewVersion = true;
            }
            return(NewVersion);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 下载
        /// </summary>
        /// <param name="WebUrl">远程路径</param>
        /// <param name="SaveName">保存的名称</param>
        /// <returns></returns>
        private bool Download(string WebUrl, string SaveName)
        {
            WebClient wc = new WebClient();

            try
            {
                ShowStr(string.Format("下载开始:{0}\t文件大小:{1}", SaveName, GetLength(WebUrl)));

                this.Dispatcher.BeginInvoke(new Action(() =>
                {
                    txt_FileName.Text  = SaveName;
                    txt_FileAmt.Text   = SizeToString(GetLength(WebUrl));
                    pb_Current.Maximum = GetLength(WebUrl);
                }));
                //调试
                if (!Directory.Exists(UpdateTempFile))
                {
                    return(false);
                }

                string DownFileName = UpdateTempFile + @"\" + SaveName.TrimStart('\\');//下载到本地的文件
                if (!Directory.Exists(System.IO.Path.GetDirectoryName(DownFileName)))
                {
                    Directory.CreateDirectory(System.IO.Path.GetDirectoryName(DownFileName));
                }

                #region  步下载
                wc.DownloadFile(WebUrl, DownFileName);//同步下载
                Console.WriteLine(string.Format("{0}\t{1} 下载结束", DateTime.Now.ToString("HH:mm:ss:fff"), SaveName));

                wc.DownloadProgressChanged += new System.Net.DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
                wc.Dispose();
                #endregion

                #region 异步下载
                //wc.DownloadFileAsync(new Uri(WebUrl), file + @"\" + savePath);//异步下载
                //wc.DownloadProgressChanged += new System.Net.DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
                //wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
                //Downloadcompleted.WaitOne();//等待异步下载完成
                #endregion
                return(true);
            }
            catch (Exception ex)
            {
                MyFunction.AppendUpdate_LOG(string.Format("下载异常:{0}", ex.Message));
                wc.Dispose();
                return(false);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// PC软件升级方法
        /// </summary>
        public void PCUpdate()
        {
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            try
            {
                string UpdateProgram = UpdateTempFile + @"\" + AppName;
                if (!File.Exists(UpdateProgram))
                {
                    ShowStr("升级程序丢失");
                    return;
                }

                string tempcopyname = "%($)%.exe";//临时复制的文件,不需要复制此文件
                File.Copy(UpdateProgram, this.ApplicationRunPath + @"\" + tempcopyname, true);
                //UpdateVersion(UpdateProgram, this.ApplicationRunPath + @"\" + AppName);
                string filePath = this.ApplicationRunPath + @"\" + tempcopyname;
                startInfo.FileName = filePath;
                //参数0为源文件夹,参数1为目标文件夹,参数2为复制完毕后运行程序
                string parameter = string.Format("{0} {1} {2}"
                                                 , this.ApplicationRunPath + @"\" + UpdateTempFile
                                                 , this.ApplicationRunPath
                                                 , AppName);
                startInfo.Arguments   = parameter;
                startInfo.WindowStyle = ProcessWindowStyle.Normal;
                Process.Start(startInfo);
            }
            catch (Exception ex)
            {
#if DEBUG
                MessageBox.Show(ex.Message, "升级故障");
#endif
                MyFunction.AppendUpdate_LOG("升级故障:" + ex.Message);
            }
            finally
            {
                Environment.Exit(0);
            }
        }