Esempio n. 1
0
        void OnDownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            DownloadFileInfo file = e.UserState as DownloadFileInfo;

            nDownloadedTotal += file.Size;
            this.SetProcessBar(0, (int)(nDownloadedTotal * 100 / total));
            LogHelper.Debug(String.Format("Finish Download:{0}", file.FileName));
            //Debug.WriteLine(String.Format("Finish Download:{0}", file.FileName));
            //替换现有文件
            string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, file.FileFullName);

            try
            {
                if (File.Exists(filePath))
                {
                    if (File.Exists(filePath + ".old"))
                    {
                        File.Delete(filePath + ".old");
                    }

                    File.Move(filePath, filePath + ".old");
                }
                File.Move(filePath + ".tmp", filePath);
            }
            catch (Exception ex)
            {
                LogHelper.Error(string.Format("move file failed: filepath【{0}】;errror msg【{1}】", filePath, ex.Message));
            }
            //继续下载其它文件
            evtPerDonwload.Set();
        }
Esempio n. 2
0
        private void ProcDownload()
        {
            evtPerDonwload = new ManualResetEvent(false);

            foreach (DownloadFileInfo file in this.downloadFileList)
            {
                total += file.Size;
            }

            while (!evtDownload.WaitOne(0, false))
            {
                if (this.downloadFileList.Count == 0)
                {
                    break;
                }

                DownloadFileInfo file = this.downloadFileList[0];

                LogHelper.Debug(String.Format("Start Download:{0}", file.FileName));
                //Debug.WriteLine(String.Format("Start Download:{0}", file.FileName));

                this.ShowCurrentDownloadFileName(file.FileName);

                //下载
                clientDownload = new WebClient();

                clientDownload.DownloadProgressChanged += new DownloadProgressChangedEventHandler(OnDownloadProgressChanged);
                clientDownload.DownloadFileCompleted   += new AsyncCompletedEventHandler(OnDownloadFileCompleted);

                evtPerDonwload.Reset();
                try
                {
                    /*
                     *  断目录存在不,不存的则添加
                     * **/
                    string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, file.FileFullName);
                    if (!File.Exists(path))
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(path));
                    }
                    clientDownload.DownloadFileAsync(new Uri(file.DownloadUrl), path + ".tmp", file);
                }
                catch (Exception ex)
                {
                    LogHelper.Error(string.Format("DownloadFileFailed:DownloadUrl【{2}】,FileName【{0}】,error Msg【{1}】", file.FileFullName, ex.Message, file.DownloadUrl));
                }

                //等待下载完成
                evtPerDonwload.WaitOne();

                clientDownload.Dispose();
                clientDownload = null;

                //移除已下载的文件
                this.downloadFileList.Remove(file);
            }
            LogHelper.Debug("All Downloaded");
            //Debug.WriteLine("All Downloaded");

            if (this.downloadFileList.Count == 0)
            {
                Exit(true);
            }
            else
            {
                Exit(false);
            }

            evtDownload.Set();
        }