Exemplo n.º 1
0
        /// <summary>
        /// 从更新缓存中替换新文件
        /// </summary>
        private void ReplaceNewFile()
        {
            List <string> exculde = new List <string>();

            exculde.Add(UpdateAppName);
            DataHelpers.MoveFolder(updateTmpPath, ".", exculde);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 检查更新
        /// </summary>
        /// <returns>是否有更新</returns>
        private bool CreateUpdate()
        {
            #region 取更新信息
            MyWebClient web = new MyWebClient {
                Proxy = WebRequest.DefaultWebProxy
            };
            string updatejson = null;
            foreach (string upInfoUrl in updateInfoUrl)
            {
                updatejson = web.DownloadString(upInfoUrl);
                if (!string.IsNullOrWhiteSpace(updatejson))
                {
                    break;
                }
            }
            #endregion

            #region 匹配文件并添加到更新列表
            string localFile = string.Empty;

            if (string.IsNullOrWhiteSpace(updatejson))
            {
                return(false);
            }

            UpdateInfo = (new MoeUpdateInfo()).GetMoeUpdateInfo(updatejson);
            if (UpdateInfo == null)
            {
                return(false);
            }

            foreach (MoeUpdateFile upfile in UpdateInfo.files)
            {
                localFile = RepairPath(upfile.Path) + upfile.Name;
                if (upfile.State == "up" || string.IsNullOrWhiteSpace(upfile.State))
                {
                    if (string.IsNullOrWhiteSpace(localFile) || string.IsNullOrWhiteSpace(upfile.Url))
                    {
                        continue;
                    }
                    if (DataHelpers.GetMD5Hash(localFile) == upfile.MD5 || DataHelpers.GetMD5Hash(updateTmpPath + "\\" + localFile) == upfile.MD5)
                    {
                        continue;
                    }
                    UpdateFilesInfo += "+ " + localFile + " - Ver " + upfile.Ver + "  (" + upfile.MD5 + ")[br]";
                    UpdateFiles.Add(upfile);
                }
                else if (upfile.State == "del" && File.Exists(localFile))
                {
                    UpdateFilesInfo += "- " + localFile + "[br]";
                    UpdateFiles.Add(upfile);
                }
            }

            return(UpdateFiles.Count > 0);

            #endregion
        }
Exemplo n.º 3
0
        /// <summary>
        /// 保存预览图到文件
        /// </summary>
        /// <param name="silent">静默模式</param>
        /// <param name="path">保存路径</param>
        /// <returns>预览文件类型</returns>
        private string SavePreview(bool silent, string path)
        {
            try
            {
                if (!((imgGrid.Children[imgs[selectedId]] as ScrollViewer).Content as PreviewImg).ImgLoaded)
                {
                    ShowMessage("图片尚未加载完毕", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return("");
                }

                PreviewImg pimg = (PreviewImg)((ScrollViewer)imgGrid.Children[imgs[selectedId]]).Content;
                switch (pimg.ImgType)
                {
                case "bmp":
                case "jpg":
                case "png":
                    Image im = (Image)pimg.prewimg.Children[0];
                    DataHelpers.ImageToFile((BitmapSource)im.Source, pimg.ImgType, path);
                    break;

                case "gif":
                    AnimatedGIF gi = (AnimatedGIF)pimg.prewimg.Children[0];
                    DataHelpers.MemoryStreamToFile((MemoryStream)gi.GIFSource, path);
                    break;

                default:
                    throw new Exception(pimg.ImgType + "类型不支持保存");
                }

                if (!silent)
                {
                    ShowMessage("已成功保存至下载文件夹", MessageBoxButton.OK, MessageBoxImage.Information);
                }

                return(pimg.ImgType);
            }
            catch (Exception ex)
            {
                if (!silent)
                {
                    ShowMessage("保存失败\r\n" + ex.Message, MessageBoxButton.OK, MessageBoxImage.Warning);
                }
                return("");
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 按顺序下载更新列表中的文件(回调)
        /// </summary>
        /// <param name="FileListCount">更新列表文件总数</param>
        /// <param name="FileListIndex">当前下载更新列表中的文件索引</param>
        private void DownloadFile(int FileListCount, int FileListIndex)
        {
            if (FileListIndex < FileListCount && UpdateState == 1)
            {
                MoeUpdateFile nowDLfile = UpdateFiles[FileListIndex];

                try
                {
                    switch (nowDLfile.State)
                    {
                    case "del":
                        #region  除信息显示
                        Dispatcher.Invoke(new Action(delegate
                        {
                            pbSingleTxt.Text         = "移除 " + nowDLfile.Name;
                            pbSingleSpeed.Visibility = pbSingleVal.Visibility = Visibility.Hidden;
                        }));
                        #endregion

                        #region  除指定文件或目录
                        string nowPath = RepairPath(nowDLfile.Path);
                        try
                        {
                            if (string.IsNullOrWhiteSpace(nowDLfile.Name))
                            {
                                //删除目录
                                Directory.Delete(nowPath, true);
                            }
                            else
                            {
                                //删除文件
                                File.Delete(nowPath + nowDLfile.Name);
                            }
                            //删除空目录
                            DirectoryInfo di = new DirectoryInfo(nowPath);
                            if (di.GetFiles().Length + di.GetDirectories().Length < 1)
                            {
                                Directory.Delete(nowPath);
                            }
                        }
                        catch { }
                        #endregion
                        break;

                    case "mov":
                        #region 移动信息显示
                        Dispatcher.Invoke(new Action(delegate
                        {
                            pbSingleTxt.Text         = "移动 " + nowDLfile.Name;
                            pbSingleSpeed.Visibility = pbSingleVal.Visibility = Visibility.Hidden;
                        }));
                        Thread.Sleep(666);
                        #endregion

                        #region 移动文件到新目录
                        string moveinfo = DataHelpers.MoveFolder(nowDLfile.Path, nowDLfile.NewPath);
                        if (!string.IsNullOrWhiteSpace(moveinfo))
                        {
                            Dispatcher.Invoke(new Action(delegate
                            {
                                pbSingleTxt.Text = moveinfo;
                            }));
                            Thread.Sleep(666);
                        }
                        #endregion
                        break;

                    default:
                        #region  载信息显示
                        Dispatcher.Invoke(new Action(delegate
                        {
                            pbSingleTxt.Text         = "获取 " + nowDLfile.Name;
                            pbSingle.Value           = 0;
                            pbSingleSpeed.Text       = "(0.00 KB/s";
                            pbSingleSpeed.Visibility = pbSingleVal.Visibility = Visibility.Visible;
                        }));
                        #endregion

                        #region 创建下载
                        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(nowDLfile.Url);
                        req.Proxy             = WebRequest.DefaultWebProxy;
                        req.UserAgent         = SessionClient.DefUA;
                        req.AllowAutoRedirect = true;

                        HttpWebResponse res = (HttpWebResponse)req.GetResponse();

                        //响应长度
                        double reslength = res.ContentLength;
                        unlimitedProgress = reslength < 1;

                        string tmpDLPath = updateTmpPath + "\\" + RepairPath(nowDLfile.Path);
                        if (!Directory.Exists(tmpDLPath))
                        {
                            Directory.CreateDirectory(tmpDLPath);
                            new DirectoryInfo(tmpDLPath).Attributes = FileAttributes.Hidden;
                        }

                        Stream str     = res.GetResponseStream();
                        Stream fileStr = new FileStream(tmpDLPath + nowDLfile.Name, FileMode.Create);
                        #endregion

                        #region 开始线程下载文件
                        //限制线程最大数
                        ThreadPool.SetMaxThreads(2, 2);

                        ThreadPool.QueueUserWorkItem((o) =>
                        {
                            byte[] buffer = new byte[1024];
                            //进度预置
                            double progressBarValue = 0;
                            //无限进度调和
                            if (unlimitedProgress)
                            {
                                reslength += buffer.Length;
                            }

                            DateTime last   = DateTime.Now;
                            int realReadLen = str.Read(buffer, 0, buffer.Length);
                            double downed   = realReadLen;
                            double speed    = -1;

                            while (realReadLen > 0 && UpdateState == 1)
                            {
                                fileStr.Write(buffer, 0, realReadLen);
                                progressBarValue += realReadLen;

                                try
                                {
                                    DateTime now = DateTime.Now;
                                    if ((now - last).TotalSeconds > 0.2)
                                    {
                                        speed  = downed / (now - last).TotalSeconds;
                                        downed = 0;
                                        last   = now;
                                    }

                                    pbSingle.Dispatcher.BeginInvoke(new ProgressBarDelegate(RefreshDownload), progressBarValue / reslength, speed);

                                    realReadLen = str.Read(buffer, 0, buffer.Length);
                                    downed     += realReadLen;
                                }
                                catch { }
                            }
                            str.Close();
                            fileStr.Close();
                        }, null);
                        #endregion
                        break;
                    }
                }
                catch (Exception ex)
                {
                    FileListIndex++;
                    Dispatcher.Invoke(new Action(delegate
                    {
                        pbSingleTxt.Text = nowDLfile.Name + ":" + ex.Message;
                        pbTotal.Value++;
                    }));
                    Thread.Sleep(666);
                }
                finally
                {
                    Dispatcher.Invoke(new Action(delegate { pbTotal.Value++; }));
                    Thread.Sleep(666);

                    //回调处理下一个文件
                    DownloadFile(FileListCount, ++FileListIndex);
                }
            }

            #region 更新结束
            if (FileListIndex >= FileListCount && UpdateState == 1)
            {
                Dispatcher.Invoke(new Action(delegate
                {
                    UpdateState = 2;
                    DownloadStateUI.Visibility = Visibility.Hidden;
                    btnY.Tag       = "y";
                    btnY.IsEnabled = true;
                    btnY.Content   = "重启完成更新";
                    btnN.Content   = "退出更新";
                }));
            }
            #endregion
        }