/// <summary> /// 下载文件的文件夹路径 /// </summary> /// <param name="file"></param> /// <param name="systemBinUrl"></param> /// <param name="tempFolder"></param> /// <returns></returns> public static string GetFolderUrl(DownloadFileInfo file, string systemBinUrl, string tempFolder) { 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 + tempFolder + folderPathUrl)) { Directory.CreateDirectory(systemBinUrl + tempFolder + folderPathUrl); } } return(folderPathUrl); }
private void ProcDownload(object o) { //临时文件夹 string tempFolderPath = Path.Combine(_SystemBinUrl, _TempFolder); if (!Directory.Exists(tempFolderPath)) { Directory.CreateDirectory(tempFolderPath); } evtPerDonwload = new ManualResetEvent(false); //文件总大小 foreach (DownloadFileInfo file in this.downloadFileList) { total += file.Size; } #region 载 try { while (!evtDownload.WaitOne(0, false)) { if (this.downloadFileList.Count == 0) { break; } //下载文件 DownloadFileInfo file = this.downloadFileList[0]; //显示下载文件名称 this.ShowCurrentDownloadFileName(file.FileName); //下载 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; ////End added //下载进度 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 { //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 } }; evtPerDonwload.Reset(); ////Download the folder file //string tempFolderPath1 = Utility.GetFolderUrl(file, _SystemBinUrl, _TempFolder); //if (!string.IsNullOrEmpty(tempFolderPath1)) //{ // tempFolderPath = Path.Combine(_SystemBinUrl, _TempFolder); // tempFolderPath += tempFolderPath1; //} //else //{ // tempFolderPath = Path.Combine(_SystemBinUrl, _TempFolder); //} string filePath = Path.Combine(tempFolderPath, file.FileFullName); DirectoryHelper.CreateDirectoryByFilePath(filePath); clientDownload.DownloadFileAsync(new Uri(file.DownloadUrl), filePath, file); //Wait for the download complete evtPerDonwload.WaitOne(); clientDownload.Dispose(); clientDownload = null; //Remove the downloaded files this.downloadFileList.Remove(file); } } catch (Exception) { ShowErrorAndRestartApplication(); //throw; } #endregion //When the files have not downloaded,return. if (downloadFileList.Count > 0) { return; } //Test network and deal with errors if there have //DealWithDownloadErrors(); #region 处理下载 this.Invoke(new MethodInvoker(delegate { buttonOk.Enabled = false; })); //Debug.WriteLine("All Downloaded"); foreach (DownloadFileInfo file in this.allFileList) { //string tempUrlPath = Utility.GetFolderUrl(file,_SystemBinUrl,_TempFolder); string oldPath = string.Empty; string newPath = string.Empty; try { oldPath = Path.Combine(_SystemBinUrl, file.FileFullName); newPath = Path.Combine(_SystemBinUrl + _TempFolder, file.FileFullName); //if (!string.IsNullOrEmpty(tempUrlPath)) //{ // oldPath = Path.Combine(_SystemBinUrl + tempUrlPath.Substring(1), file.FileName); // newPath = Path.Combine(_SystemBinUrl + _TempFolder + tempUrlPath, file.FileName); //} //else //{ // oldPath = Path.Combine(_SystemBinUrl, file.FileName); // newPath = Path.Combine(_SystemBinUrl + _TempFolder, 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("config_")) { 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 Utility.MoveFolderToOld(oldPath, newPath); //if (File.Exists(oldPath)) //{ // Utility.MoveFolderToOld(oldPath, newPath); //} //else //{ // //Edit for config_ file // if (!string.IsNullOrEmpty(tempUrlPath)) // { // if (!Directory.Exists(_SystemBinUrl + tempUrlPath.Substring(1))) // { // Directory.CreateDirectory(_SystemBinUrl + tempUrlPath.Substring(1)); // Utility.MoveFolderToOld(oldPath, newPath); // } // else // { // Utility.MoveFolderToOld(oldPath, newPath); // } // } // else // { // Utility.MoveFolderToOld(oldPath, newPath); // } //} } catch (Exception) { //log the error message,you can use the application's log code } } #endregion //After dealed with all files, clear the data this.allFileList.Clear(); if (this.downloadFileList.Count == 0) { Exit(true); } else { Exit(false); } evtDownload.Set(); }