public void Remove(string Path, string Filename) { try { if (_ftpClient.DirectoryExists(Path)) { _ftpClient.RetryAttempts = 10; string fullpath = Path + Filename; _log.Info($"{FtpTemplateMessage("Remove", Filename)}", fullpath); _ftpClient.DeleteFile(fullpath); } else { _log.Info($"{FtpTemplateMessage("Remove", Filename, "not exist")}", Path + Filename); throw new Exception("File Not Exist"); } } catch (Exception ex) { _log.Error($"{FtpTemplateMessage("Remove", Filename, "throw exception")}", ex); throw ex; } }
private void SyncDirectory(IFtpClient client, string remotePath, string localPath, bool isDel) { var localFolder = new DirectoryInfo(localPath); var infos = localFolder.GetFileSystemInfos(); foreach (var info in infos) { if (!client.IsConnected) { client.Connect(); } if (info is FileInfo) { var size = (info as FileInfo).Length; var remoteFile = Path.Combine(remotePath, info.Name); if (!client.FileExists(remoteFile) || client.GetFileSize(remoteFile) != size) { client.UploadFile(info.FullName, remoteFile); Logger.Info($"Uploaded==>{info.FullName}"); } } else if (info is DirectoryInfo) { var remoteFile = Path.Combine(remotePath, info.Name); if (!client.DirectoryExists(remoteFile)) { client.CreateDirectory(remoteFile); Logger.Info($"CreateFtpDirectory==>{remoteFile}"); } SyncDirectory(client, Path.Combine(remotePath, info.Name), info.FullName, isDel); } } if (isDel) { var items = client.GetListing(remotePath); foreach (var item in items) { if (infos.All(info => info.Name != item.Name)) { if (item.Type == FtpFileSystemObjectType.File) { client.DeleteFile(item.FullName); } else if (item.Type == FtpFileSystemObjectType.Directory) { client.DeleteDirectory(item.FullName); } Logger.Info($"DeletedFtp==>{item.FullName}"); } } } }
protected virtual async Task <ImportVehiclesForSiteResult> InternalImportForSiteAsync(Site site, IFtpClient ftpClient) { ImportVehiclesForSiteResult importResult; try { if (site.ImportRelativeFtpPath != null) { if (ftpClient.DirectoryExists(site.ImportRelativeFtpPath)) { FtpListItem lastModifiedFileInfo = (await ftpClient.GetListingAsync(site.ImportRelativeFtpPath, FtpListOption.Auto)) .Where(r => r.Type == FtpFileSystemObjectType.File) .OrderByDescending(r => r.Modified) .FirstOrDefault(); if (lastModifiedFileInfo != null) { IEnumerable <Vehicle> vehicles; using (var csvFileStream = new MemoryStream(await ftpClient.DownloadAsync(lastModifiedFileInfo.FullName))) { vehicles = VehicleBulkFactory.Create(new VehicleFromCsvFileBulkFactorySettings(site.Id, csvFileStream)); } await VehicleRepository.RefreshEntitiesForSiteAsync(site.Id, vehicles); importResult = new ImportVehiclesForSiteResult( site.Id, site.Name, vehicles.Count(), vehicles.Count(r => r.Condition == VehicleConditions.New), vehicles.Count(r => r.Condition == VehicleConditions.Used)); } else { importResult = new ImportVehiclesForSiteResult(site.Id, site.Name, $"No files in specified folder ({site.ImportRelativeFtpPath}).", ImportStatusEnum.NotStarted); } } else { importResult = new ImportVehiclesForSiteResult(site.Id, site.Name, $"The specified folder ({site.ImportRelativeFtpPath}) was not found.", ImportStatusEnum.NotStarted); } } else { importResult = new ImportVehiclesForSiteResult(site.Id, site.Name, $"The specified folder is not defined.", ImportStatusEnum.NotStarted); } } catch (Exception ex) { importResult = new ImportVehiclesForSiteResult(site.Id, site.Name, ex.Message, ImportStatusEnum.Failed); } return(importResult); }
public void UploadFiles(string[] FilePaths, TreeNode pNode) { IFtpClient pFtpClient = SysDBConfig.GetInstance().GetFtpClient("CHXQ"); pFtpClient.Connect(); IDataBase pDataBase = SysDBConfig.GetInstance().GetOleDataBase("OrclConn"); pDataBase.OpenConnection(); string DirPath = GetFtpPathByNode(pNode); try { if (!pFtpClient.DirectoryExists(DirPath)) { pFtpClient.CreateDirectory(DirPath); } foreach (string FilePath in FilePaths) { string RemotePath = string.Format("{0}\\{1}", DirPath, System.IO.Path.GetFileName(FilePath)); pFtpClient.UploadFile(FilePath, RemotePath); string ProcedureName = "AddFile"; IDataParameter[] DataParameters = new IDataParameter[] { new OracleParameter() { ParameterName = "V_FileName", OracleType = OracleType.NVarChar, Value = System.IO.Path.GetFileNameWithoutExtension(FilePath) }, new OracleParameter() { ParameterName = "V_Path", OracleType = OracleType.NVarChar, Value = RemotePath }, new OracleParameter() { ParameterName = "V_DirID", OracleType = OracleType.Number, Value = this.GetNodeID(pNode) } }; pDataBase.ExecuteProcedure(ProcedureName, ref DataParameters); } } catch (Exception ex) { throw ex; } finally { pDataBase.CloseConnection(); pFtpClient.Close(); } }
private void UploadFile(string FilePath, string UploadPath) { IFtpClient pFtpClient = HR.Utility.SysDBConfig.GetInstance().GetFtpClient("CHXQ"); pFtpClient.Connect(); string Dir = System.IO.Path.GetDirectoryName(UploadPath); if (!pFtpClient.DirectoryExists(Dir)) { pFtpClient.CreateDirectory(Dir); } if (pFtpClient.Exists(UploadPath)) { pFtpClient.DeleteFile(UploadPath); } pFtpClient.UploadFile(FilePath, UploadPath); pFtpClient.Close(); }
private void CreateMissingDirectories(string serverPath, IFtpClient client) { var pathItems = serverPath.SplitPath(); var serverLocation = string.Empty; foreach (var pathItem in pathItems) { serverLocation = serverLocation.CombinePath(pathItem); var exists = client.DirectoryExists(serverLocation); if (exists) { continue; } var creationResult = client.CreateDirectory(serverLocation); if (creationResult == false) { Debug.LogError($"Can't create Remote Folder {serverLocation}"); } } }