Beispiel #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);
   //  folderPathUrl = file.FileName;
      
     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;
 }
Beispiel #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))
                {
                    if (this.downloadFileList.Count == 0)
                    {
                        break;
                    }

                    DownloadFileInfo file = this.downloadFileList[0];

                    Debug.Print(String.Format("Start Download:{0}", file.FileName));
                    Invoke(new Action(() =>
                    {
                        label7.Text = String.Format("下载:{0}中", file.FileName);
                        //txtsouce.ScrollToCaret();
                    }));
                    this.ShowCurrentDownloadFileName(file.FileName);

                    //Download
                    clientDownload = new WebClient();

                    //Added the function to support proxy
                    // clientDownload.Proxy = System.Net.WebProxy.GetDefaultProxy();
                    clientDownload.Proxy.Credentials = CredentialCache.DefaultCredentials;

                    clientDownload.Credentials = System.Net.CredentialCache.DefaultCredentials;
                    clientDownload.Headers.Add("Cache-Control", "no-cache");
                    //End added
                    #region DownloadProgressChanged事件设置

                    clientDownload.DownloadProgressChanged += (object sender, DownloadProgressChangedEventArgs e) =>
                    {
                        try
                        {
                            this.SetProcessBar(e.ProgressPercentage, (int)((nDownloadedTotal + e.BytesReceived) * 100 / total));
                        }
                        catch
                        {
                            //log the error message,you can use the application's log code
                        }
                    };

                    clientDownload.DownloadFileCompleted += (object sender, AsyncCompletedEventArgs e) =>
                    {
                        try
                        {
                            //此处可以捕获e.Error错误
                            DealWithDownloadErrors();
                            DownloadFileInfo dfile = e.UserState as DownloadFileInfo;
                            nDownloadedTotal += dfile.Size;
                            this.SetProcessBar(0, (int)(nDownloadedTotal * 100 / total));
                            evtPerDonwload.Set();
                        }
                        catch (Exception)
                        {
                            //log the error message,you can use the application's log code
                        }
                    };
                    #endregion

                    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);
                    }

                    //2014-05-15 新增 XuWangBin 说明有子目录
                    if (file.FileFullName.Contains(@"\"))
                    {
                        string tmpDir = Path.Combine(tempFolderPath, file.FileFullName.Substring(0, file.FileFullName.LastIndexOf(@"\")));
                        if (!Directory.Exists(tmpDir))
                        {
                            Directory.CreateDirectory(tmpDir);
                        }
                    }

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

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

                    clientDownload.Dispose();
                    clientDownload = null;

                    //Remove the downloaded files
                    this.downloadFileList.Remove(file);
                    //Thread.Sleep(200);
                }
            }
            catch (Exception ex)
            {
                Msg.DisplayMsg(ex.StackTrace, ex.Message, 8000);
                //  ShowErrorAndRestartApplication();
                //throw;
            }

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

            //Test network and deal with errors if there have
            DealWithDownloadErrors();

            //Debug.WriteLine("All Downloaded");
            foreach (DownloadFileInfo file in this.allFileList)
            {
                string tempUrlPath = CommonUnitity.GetFolderUrl(file);
                string oldPath     = string.Empty;
                string newPath     = string.Empty;
                try
                {
                    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);
                    }

                    //just deal with the problem which the files EndsWith xml can not download
                    System.IO.FileInfo f = new FileInfo(newPath);
                    if (!file.Size.ToString().Equals(f.Length.ToString()) && !file.FileName.ToString().EndsWith(".xml"))
                    {
                        ShowErrorAndRestartApplication();
                    }

                    //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
                {
                    //log the error message,you can use the application's log code
                }
            }

            //After dealed with all files, clear the data
            this.allFileList.Clear();
            if (this.downloadFileList.Count == 0)
            {
                Exit(true);
            }
            else
            {
                Exit(false);
            }

            evtDownload.Set();
        }