示例#1
0
        public static string GetFolderUrl(DownloadFileInfo file)
        {
            string folderPathUrl    = string.Empty;
            int    folderPathPoint  = file.DownloadUrl.IndexOf("/", 15) + 1;
            string filepathstring   = file.DownloadUrl.Substring(folderPathPoint);
            int    folderPathPoint1 = filepathstring.IndexOf("/");
            string filepathstring1  = filepathstring.Substring(folderPathPoint1 + 1);

            if (filepathstring1.IndexOf("/") != -1)
            {
                string[] ExeGroup = filepathstring1.Split('/');
                for (int i = 0; i < ExeGroup.Length - 1; i++)
                {
                    folderPathUrl += "\\" + ExeGroup[i];
                }
                if (!Directory.Exists(SystemBinUrl + ConstFile.TEMPFOLDERNAME + folderPathUrl))
                {
                    Directory.CreateDirectory(SystemBinUrl + ConstFile.TEMPFOLDERNAME + folderPathUrl);
                }
            }
            return(folderPathUrl);
        }
示例#2
0
        private void ProcDownload(object o)
        {
            string tempFolderPath = Path.Combine(CommonUnitity.SystemBinUrl, ConstFile.TEMPFOLDERNAME);

            if (!Directory.Exists(tempFolderPath))
            {
                Directory.CreateDirectory(tempFolderPath);
            }


            evtPerDonwload = new ManualResetEvent(false);

            foreach (DownloadFileInfo file in this.downloadFileList)
            {
                total += file.Size;
            }
            try
            {
                while (!evtDownload.WaitOne(0, false) && !bCancel)
                {
                    if (this.downloadFileList.Count == 0)
                    {
                        break;
                    }
                    this.SetProcessBar(100, (int)(nDownloadedTotal * 100 / total));
                    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);

                    //Download

                    using (clientDownload = new HttpWebClient())
                    {
                        //Added the function to support proxy
                        clientDownload.Proxy = null;
                        //clientDownload.Proxy.Credentials = CredentialCache.DefaultCredentials;
                        //clientDownload.Credentials = System.Net.CredentialCache.DefaultCredentials;
                        //End added

                        clientDownload.DownloadProgressChanged +=
                            (object sender, DownloadProgressChangedEventArgs e) =>
                        {
                            try
                            {
                                this.SetProcessBar(e.ProgressPercentage,
                                                   (int)
                                                   ((nDownloadedTotal +
                                                     e.BytesReceived) * 100 /
                                                    total));
                            }
                            catch (Exception ex)
                            {
                                LogHelper.Error(ex);
                                //EventLog.WriteEntry("DownloadProgress", ex.Message,
                                //                   EventLogEntryType.Error);
                            }
                        };

                        clientDownload.DownloadFileCompleted += (object sender, AsyncCompletedEventArgs e) =>
                        {
                            try
                            {
                                //DealWithDownloadErrors();
                                DownloadFileInfo dfile =
                                    e.UserState as DownloadFileInfo;
                                try
                                {
                                    string newPath = Path.Combine(
                                        tempFolderPath,
                                        dfile.FileFullName);
                                    FileInfo f = new FileInfo(newPath);
                                    if (dfile.Size != f.Length)
                                    {
                                        LogHelper.Error(
                                            string.Format(
                                                "{0}.Size({1})!={2}.Size({3})",
                                                dfile.FileName,
                                                dfile.Size, f.Name,
                                                f.Length));
                                        LogHelper.Error(e.Error);
                                        isRetry = true;
                                        retryCount++;
                                    }
                                    else
                                    {
                                        isRetry    = false;
                                        retryCount = 0;
                                        LogHelper.Debug(
                                            string.Format(
                                                "Download {0} Success",
                                                dfile.FileName));
                                    }
                                }
                                catch (Exception ex)
                                {
                                    isRetry = true;
                                    retryCount++;
                                    LogHelper.Debug(
                                        string.Format(
                                            "Download {0} Failed",
                                            dfile.FileName));
                                    LogHelper.Error(ex);
                                }
                                if (!isRetry)
                                {
                                    nDownloadedTotal += dfile.Size;
                                    this.SetProcessBar(100,
                                                       (int)
                                                       (nDownloadedTotal *
                                                        100 /
                                                        total));
                                }



                                evtPerDonwload.Set();
                            }
                            catch (Exception ex)
                            {
                                isRetry = true;
                                retryCount++;
                                LogHelper.Error(ex);
                            }
                        };

                        evtPerDonwload.Reset();

                        //Download the folder file
                        string tempFolderPath1 = CommonUnitity.GetFolderUrl(file);
                        if (!string.IsNullOrEmpty(tempFolderPath1))
                        {
                            tempFolderPath  = Path.Combine(CommonUnitity.SystemBinUrl, ConstFile.TEMPFOLDERNAME);
                            tempFolderPath += tempFolderPath1;
                        }
                        else
                        {
                            tempFolderPath = Path.Combine(CommonUnitity.SystemBinUrl, ConstFile.TEMPFOLDERNAME);
                        }

                        clientDownload.DownloadFileAsync(new Uri(file.DownloadUrl),
                                                         Path.Combine(tempFolderPath, file.FileFullName), file);

                        //Wait for the download complete
                        evtPerDonwload.WaitOne();


                        if (isRetry)
                        {
                            LogHelper.Debug(string.Format("{0} retry Download[retry count:{1}]", file.FileName,
                                                          retryCount));
                        }

                        if (!isRetry)
                        {
                            this.downloadFileList.Remove(file);
                        }
                        else if (retryCount >= 5)
                        {
                            LogHelper.Debug("Retry Timeout");
                            bCancel = true;
                        }
                    }
                }
                System.GC.Collect();

                if (bCancel)
                {
                    if (retryCount == 5)
                    {
                        CommonUnitity.ShowErrorAndRestartApplication(string.Format("{0}更新不成功[网络异常,请确保网络环境稳定],{0}现在将重新启动,尝试再次更新,请单击确定重新启动程序!", ConstFile.AppName, ""), true);
                    }
                    CommonUnitity.ShowErrorAndRestartApplication(string.Format("{0}更新不成功[{1}],{0}现在将重新启动,尝试再次更新,请单击确定重新启动程序!", ConstFile.AppName, "用户取消升级"), true);
                }
            }
            catch (Exception ex)
            {
                //EventLog.WriteEntry("DownloadProgress", ex.Message,
                LogHelper.Error(ex);                                                                                  //EventLogEntryType.Error);
                CommonUnitity.ShowErrorAndRestartApplication(string.Format("{0}[{1}]", ConstFile.NOTNETWORK, ex.Message), true);
                //throw;
            }

            //When the files have not downloaded,return.
            if (downloadFileList.Count > 0)
            {
                return;
            }


            foreach (DownloadFileInfo file in this.allFileList)
            {
                string tempUrlPath = CommonUnitity.GetFolderUrl(file);
                string oldPath     = string.Empty;
                string newPath     = string.Empty;
                if (!string.IsNullOrEmpty(tempUrlPath))
                {
                    oldPath = Path.Combine(CommonUnitity.SystemBinUrl + tempUrlPath.Substring(1), file.FileName);
                    newPath = Path.Combine(CommonUnitity.SystemBinUrl + ConstFile.TEMPFOLDERNAME + tempUrlPath, file.FileName);
                }
                else
                {
                    oldPath = Path.Combine(CommonUnitity.SystemBinUrl, file.FileName);
                    newPath = Path.Combine(CommonUnitity.SystemBinUrl + ConstFile.TEMPFOLDERNAME, file.FileName);
                }
            }

            LogHelper.Debug("All Downloaded");

            try
            {
                foreach (DownloadFileInfo file in this.allFileList)
                {
                    string tempUrlPath = CommonUnitity.GetFolderUrl(file);
                    string oldPath     = string.Empty;
                    string newPath     = string.Empty;

                    if (!string.IsNullOrEmpty(tempUrlPath))
                    {
                        oldPath = Path.Combine(CommonUnitity.SystemBinUrl + tempUrlPath.Substring(1), file.FileName);
                        newPath = Path.Combine(CommonUnitity.SystemBinUrl + ConstFile.TEMPFOLDERNAME + tempUrlPath, file.FileName);
                    }
                    else
                    {
                        oldPath = Path.Combine(CommonUnitity.SystemBinUrl, file.FileName);
                        newPath = Path.Combine(CommonUnitity.SystemBinUrl + ConstFile.TEMPFOLDERNAME, file.FileName);
                    }
                    //Added for dealing with the config file download errors
                    string newfilepath = string.Empty;
                    if (newPath.Substring(newPath.LastIndexOf(".") + 1).Equals(ConstFile.CONFIGFILEKEY))
                    {
                        if (System.IO.File.Exists(newPath))
                        {
                            if (newPath.EndsWith("_"))
                            {
                                newfilepath = newPath;
                                newPath     = newPath.Substring(0, newPath.Length - 1);
                                oldPath     = oldPath.Substring(0, oldPath.Length - 1);
                            }
                            File.Move(newfilepath, newPath);
                        }
                    }
                    //End added

                    if (File.Exists(oldPath))
                    {
                        MoveFolderToOld(oldPath, newPath);
                    }
                    else
                    {
                        //Edit for config_ file
                        if (!string.IsNullOrEmpty(tempUrlPath))
                        {
                            if (!Directory.Exists(CommonUnitity.SystemBinUrl + tempUrlPath.Substring(1)))
                            {
                                Directory.CreateDirectory(CommonUnitity.SystemBinUrl + tempUrlPath.Substring(1));
                                MoveFolderToOld(oldPath, newPath);
                            }
                            else
                            {
                                MoveFolderToOld(oldPath, newPath);
                            }
                        }
                        else
                        {
                            MoveFolderToOld(oldPath, newPath);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                CommonUnitity.ShowErrorAndRestartApplication(string.Format("{0}更新不成功[未知异常,请重启电脑或手动删除程序目录下面后缀为.old的所有文件],请单击退出程序", ConstFile.AppName), false);
            }
            finally
            {
                this.allFileList.Clear();

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

                evtDownload.Set();
            }
        }