public bool DownloadFile(DIFileInfo fileInfo, string updateFolderPath) { bool RetVal = false; string FileToDownload = string.Empty; string TempFileName = string.Empty; string FtpDirectory = string.Empty; string LocalFileName = string.Empty; try { FileToDownload = Path.GetFileName(fileInfo.FileName) + ".zip"; //-- If this file belongs to current Adaptation, then handle Adaptation Folder name in downloaded path. if (fileInfo.ApplicationName.ToLower() == this.Adaptation.ToLower()) { TempFileName = Path.Combine(Path.Combine(updateFolderPath, fileInfo.ApplicationName), Path.GetDirectoryName(fileInfo.FilePath)) + "\\" + Path.GetFileNameWithoutExtension(fileInfo.FileName) + ".zip"; } else { if (fileInfo.FileName.ToLower() == "diLiveUpdate.exe".ToLower()) { //-- NOTE: Special handling for "diLiveUpdate.exe" . // as "diLiveUpdate.exe" needs to be udpated directly into root application Path folder // instead of Temp Liveupdate Folder. TempFileName = Path.Combine(this.AppInstallationPath, Path.GetFileNameWithoutExtension(fileInfo.FileName) + ".zip"); } else { //-- normal files. TempFileName = Path.Combine(updateFolderPath, Path.GetDirectoryName(fileInfo.FilePath)) + "\\" + Path.GetFileNameWithoutExtension(fileInfo.FileName) + ".zip"; } } // -- Concatenate Server root FTP path with file's parent folder path. FtpDirectory = this.FTPDirectoryName + "/" + fileInfo.ApplicationName + "/" + this.GetFTPFileReferencePath(fileInfo.FilePath); // --Download it from location specified in the same xml element // --and save in temp folder RetVal = this.DownloadFileThroughFTP(FileToDownload, FtpDirectory, TempFileName); //-- Change ModifiedDateTime of downloaded file to server's file Modified datatime. //-- because when file are unzipped, then creation time becomes current system time string downloadedFile = Path.Combine(Path.GetDirectoryName(TempFileName), fileInfo.FileName); File.SetLastWriteTime(downloadedFile, Convert.ToDateTime(fileInfo.ModifiedDateTimeStamp)); //-- Unzip "Zipdi" files as Special handling if (Path.GetExtension(fileInfo.FileName).ToLower() == ".zipdi") { //-- unzip file again (because .zipdi files were zipped twice at server FTPClient oftp = new FTPClient(); TempFileName = Path.Combine(Path.GetDirectoryName(TempFileName), fileInfo.FileName); oftp.ExtractArchive(TempFileName, Path.GetDirectoryName(TempFileName), false); } } catch (Exception ex) { RetVal = false; ExceptionHandler.ExceptionFacade.ThrowException(ex); //MessageBoxControl.ShowMsg(DIMessage.CONNECTION_FAILED, Msg_IconType.INFORMATION, Msg_ButtonType.OK, true); } return RetVal; }
/// <summary> /// Downloads the specified file from FTP server . FTP directory must be specified. /// </summary> /// <param name="ftpFileName">ftp file name to download.</param> /// <param name="ftpDirectory">ftp directpry path.</param> /// <param name="localFileName">desired local filename for downloaded file.</param> /// <returns>true, if succesfully downloaded.</returns> public bool DownloadFileThroughFTP(string ftpFileName, string ftpDirectory, string localFileName) { bool RetVal = false; if (this.DIFTPClient == null) { this.DIFTPClient = new FTPClient(); this.DIFTPClient.DownlaodStatusEvent += new FTPClient.DownlaodStatusDelegate(DIFTPClient_DownlaodStatusEvent); this.DIFTPClient.DownloadCancelled += new EventHandler(DIFTPClient_DownloadCancelled); } this.DIFTPClient.CancelDownloadAsync = false; //- Delete local file if exists try { if (Directory.Exists(Path.GetDirectoryName(localFileName)) == false) { Directory.CreateDirectory(Path.GetDirectoryName(localFileName)); } else if (File.Exists(localFileName)) { File.Delete(localFileName); } } catch { } //--Download File from the ftp location. try { this.DIFTPClient.RemoteHost = this.FTPServerName; // RemoteHost Server this.DIFTPClient.UserName = this.FTPUserName; this.DIFTPClient.Password = this.FTPPassCode; //Check the existence of port number. this.DIFTPClient.RemotePort = Convert.ToInt32(DILiveUpdate.FTPPort); this.DIFTPClient.RemotePath = ftpDirectory; this.DIFTPClient.Download(ftpFileName, localFileName); //this.DIFTPClient.Close(); RetVal = true; } catch (Exception ex) { this.DIFTPClient.cleanup(); this.DIFTPClient = null; this.DeleteFile(localFileName); throw new ApplicationException(ex.Message.ToString()); } return RetVal; }
public void CloseFTP() { try { if (this.DIFTPClient != null) { this.DIFTPClient.Close(); this.DIFTPClient = null; } } catch (Exception ex) { DILiveUpdate.LiveUpdateLog += "Closing FTP Connection - Failed" + Environment.NewLine; DILiveUpdate.LiveUpdateLog += "--ERROR -- " + ex.Message.ToString() + Environment.NewLine; } }