public void RollBack() { foreach (DownloadFileInfo file in _downloadFileListTemp) { string tempUrlPath = DownloadFileInfo.GetFolderUrl(file); string oldPath = string.Empty; try { if (!string.IsNullOrEmpty(tempUrlPath)) { oldPath = Path.Combine(CommonUnitity.SystemBinUrl + tempUrlPath.Substring(1), file.FileName); } else { oldPath = Path.Combine(CommonUnitity.SystemBinUrl, file.FileName); } if (oldPath.EndsWith("_")) { oldPath = oldPath.Substring(0, oldPath.Length - 1); } MoveFolderToOld(oldPath + ".old", oldPath); } catch (Exception ex) { //log the error message,you can use the application's log code } } }
public static string GetFolderUrl(DownloadFileInfo file) { if (file == null || string.IsNullOrEmpty(file.FileFullName)) { return(""); } string folderPathUrl = string.Empty; if (file.FileFullName.IndexOf("/") != -1) { string[] ExeGroup = file.FileFullName.Split('/'); for (int i = 0; i < ExeGroup.Length - 1; i++) { folderPathUrl += "\\" + ExeGroup[i]; } if (!Directory.Exists(CommonUnitity.SystemBinUrl + Constant.Tempfoldername + folderPathUrl)) { Directory.CreateDirectory(CommonUnitity.SystemBinUrl + Constant.Tempfoldername + folderPathUrl); } } return(folderPathUrl); }
private void ProcDownload(object o) { string tempFolderPath = Path.Combine(CommonUnitity.SystemBinUrl, Constant.Tempfoldername); if (!Directory.Exists(tempFolderPath)) { Directory.CreateDirectory(tempFolderPath); } _evtPerDonwload = new ManualResetEvent(false); foreach (DownloadFileInfo file in _downloadFileList) { _total += file.Size; } CommonUnitity.OnLog(this, new EventArgs <string>("tempFolderPath=" + tempFolderPath)); try { while (!_evtDownload.WaitOne(0, false)) { if (_downloadFileList.Count == 0) { break; } DownloadFileInfo file = _downloadFileList[0]; CommonUnitity.OnLog(this, new EventArgs <string>(String.Format("Start Download: url={0}, fullname={1}", file.DownloadUrl, file.FileFullName))); ShowCurrentDownloadFileName(file.FileName); //Download _clientDownload = new WebClient(); //Added the function to support proxy _clientDownload.Proxy = WebProxy.GetDefaultProxy(); _clientDownload.Proxy.Credentials = CredentialCache.DefaultCredentials; _clientDownload.Credentials = CredentialCache.DefaultCredentials; //End added _clientDownload.DownloadProgressChanged += (sender, e) => { try { SetProcessBar(e.ProgressPercentage, (int)((_nDownloadedTotal + e.BytesReceived) * 100 / _total)); } catch { CommonUnitity.OnLog(this, new EventArgs <string>("progress changed failed." + e.ProgressPercentage + "/" + e.TotalBytesToReceive)); //log the error message,you can use the application's log code } }; _clientDownload.DownloadFileCompleted += (sender, e) => { try { DealWithDownloadErrors(); DownloadFileInfo dfile = e.UserState as DownloadFileInfo; _nDownloadedTotal += dfile.Size; SetProcessBar(0, (int)(_nDownloadedTotal * 100 / _total)); _evtPerDonwload.Set(); CommonUnitity.OnLog(this, new EventArgs <string>("downloadFinished:" + dfile.FileFullName)); } catch (Exception ex) { CommonUnitity.OnLog(this, new EventArgs <string>(ex.ToString())); //log the error message,you can use the application's log code } }; _evtPerDonwload.Reset(); //Download the folder file string tempFolderPath1 = DownloadFileInfo.GetFolderUrl(file); if (!string.IsNullOrEmpty(tempFolderPath1)) { tempFolderPath = Path.Combine(CommonUnitity.SystemBinUrl, Constant.Tempfoldername); tempFolderPath += tempFolderPath1; } else { tempFolderPath = Path.Combine(CommonUnitity.SystemBinUrl, Constant.Tempfoldername); } _clientDownload.DownloadFileAsync(new Uri(file.DownloadUrl), Path.Combine(tempFolderPath, file.FileName), file); //Wait for the download complete _evtPerDonwload.WaitOne(); _clientDownload.Dispose(); _clientDownload = null; //Remove the downloaded files _downloadFileList.Remove(file); } } catch (Exception ex) { Console.WriteLine(ex); Console.WriteLine(ex.Source); CommonUnitity.OnLog(this, new EventArgs <string>(ex.StackTrace)); ShowErrorAndExitApp(); //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 _allFileList) { string tempUrlPath = DownloadFileInfo.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 + Constant.Tempfoldername + tempUrlPath, file.FileName); } else { oldPath = Path.Combine(CommonUnitity.SystemBinUrl, file.FileName); newPath = Path.Combine(CommonUnitity.SystemBinUrl + Constant.Tempfoldername, file.FileName); } // //just deal with the problem which the files EndsWith xml can not download // FileInfo f = new FileInfo(newPath); // if (!file.Size.ToString().Equals(f.Length.ToString()) && !file.FileName.EndsWith(".xml")) // { // Console.WriteLine("download failed: {0}", f.FullName); // ShowErrorAndExitApp(); // } //Added for dealing with the config file download errors string newfilepath = string.Empty; if (newPath.Substring(newPath.LastIndexOf(".") + 1).Equals(Constant.Configfilekey)) { if (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 exp) { CommonUnitity.OnLog(this, new EventArgs <string>(exp.ToString())); //log the error message,you can use the application's log code } } //After dealed with all files, clear the data _allFileList.Clear(); if (_downloadFileList.Count == 0) { Exit(true); } else { Exit(false); } _evtDownload.Set(); }