예제 #1
0
 List<UpFileInfo> GetUpdateFiles()
 {
     List<UpFileInfo> lstUpFile = new List<UpFileInfo>();
     string xmlPath = Path.Combine(ClientUpFolder + AppUpdaterXMLFileName);
     if (!File.Exists(xmlPath))
     {
         throw new Exception("更新目录Xml文件不存在");
     }
     XDocument objDoc = XDocument.Load(xmlPath);
     foreach (XElement objElem in objDoc.Document.Element("update").Element("files").Elements("file"))
     {
         UpFileInfo objUpFile = new UpFileInfo();
         objUpFile.FileName = objElem.Attribute("path").Value;
         objUpFile.Version = objElem.Attribute("ver").Value;
         FileInfo objFile = new FileInfo(Path.Combine(ClientUpFolder, objElem.Attribute("path").Value));
         if (objFile.Exists)
             objUpFile.FileLength = objFile.Length;
         else
             objUpFile.FileLength = -1;
         lstUpFile.Add(objUpFile);
     }
     return lstUpFile;
 }
예제 #2
0
 /// <summary>
 /// 获取本地Xml目录
 /// </summary>
 /// <returns></returns>
 private List<UpFileInfo> GetLocalUpFileList()
 {
     List<UpFileInfo> lstUpFile = new List<UpFileInfo>();
     if (!File.Exists(ConfigSet.Current.XmlFileName))
     {
         return new List<UpFileInfo>();
     }
     XDocument objDoc = XDocument.Load(ConfigSet.Current.XmlFileName);
     foreach (XElement objElem in objDoc.Document.Element("update").Element("files").Elements("file"))
     {
         UpFileInfo objUpFile = new UpFileInfo();
         objUpFile.FileName = objElem.Attribute("path").Value;
         objUpFile.Version = objElem.Attribute("ver").Value;
         FileInfo objFile = new FileInfo(objElem.Attribute("path").Value);
         if (objFile.Exists)
             objUpFile.FileLength = objFile.Length;
         else
             objUpFile.FileLength = -1;
         lstUpFile.Add(objUpFile);
     }
     return lstUpFile;
 }
예제 #3
0
        /// <summary>
        /// 下载更新文件
        /// </summary>
        /// <param name="objUpFile"></param>
        private void DownloadFile(UpFileInfo objUpFile)
        {
            Stream sourceStream = null;
            try
            {
                sourceStream = ServiceProxy.Current.CreateUpdateFileStream(objUpFile.FileName);
            }
            catch (Exception ex)
            {
                this.Dispatcher.Invoke(new Action(() =>
                {
                    MessageBox.Show("编码:ServiceProxy.CreateUpdateFileStream\r\n描述:" + ex.Message);
                    UpdaterStatus = EFileStatus.Error;
                    this.Close();
                }));
            }

            //创建文件夹
            Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(ConfigSet.Current.TempFolder + objUpFile.FileName)));
            using (var targetStream = new FileStream(ConfigSet.Current.TempFolder + objUpFile.FileName, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                //定义文件缓冲区
                byte[] buffer = new byte[ConfigSet.Current.BufferSize];
                int count = 0;

                long totalDownloadedByte = 0;
                while ((count = sourceStream.Read(buffer, 0, (int)ConfigSet.Current.BufferSize)) > 0)
                {
                    //总下载字节数
                    totalDownloadedByte = count + totalDownloadedByte;
                    targetStream.Write(buffer, 0, count);
                    targetStream.Flush();

                    if (ConfigSet.Current.DelayMillisecond > 0)
                    {
                        Thread.Sleep(ConfigSet.Current.DelayMillisecond);
                    }

                    this.Dispatcher.Invoke(new Action(() =>
                    {
                        long totalDownloadKB = (long)Math.Floor((double)totalDownloadedByte / 1024);
                        //百分比,小于等于最大整数
                        int percent = (int)Math.Floor((double)totalDownloadedByte / (double)objUpFile.FileLength * 100);

                        DataTable dt = gridControl.ItemsSource as DataTable;
                        foreach (DataRow objRow in dt.Rows)
                        {
                            if (objRow[0].ToString() == objUpFile.FileName)
                            {
                                objRow[2] = totalDownloadKB.ToString() + "K";
                                objRow[3] = percent.ToString() + "%";
                                break;
                            }
                        }
                    }));
                }
                targetStream.Close();
                sourceStream.Close();
            }

            //下载完成后,检查并更新
            this.Dispatcher.Invoke(new Action(() =>
            {
                DataTable dt = gridControl.ItemsSource as DataTable;
                foreach (DataRow objRow in dt.Rows)
                {
                    if (objRow[0].ToString() == objUpFile.FileName)
                    {
                        DownloadedFileNum++; //下载完成数
                        DownloadingThreadNum--; //正在更新的线程数
                        this.Title = String.Format("开始下载...   已完成[{0}/{1}]   活动线程数[{2}]",
                            DownloadedFileNum, UpdateUpFileList.Count, DownloadingThreadNum);
                        break;
                    }
                }
            }));
        }