public bool Upload(byte[] uploadedFile, string Path, string Filename, bool withCreateDirectory = false) { try { _ftpClient.RetryAttempts = 10; string fullpath = Path + Filename; if (_ftpClient.DirectoryExists(Path) == false) { if (withCreateDirectory == true) { _ftpClient.CreateDirectory(Path); } } using (MemoryStream ms = new MemoryStream()) { _log.Info($"Uploading File <{Filename}> on FTP <{_options.Host}>", fullpath); return(_ftpClient.Upload(uploadedFile, fullpath, FtpExists.Overwrite, false)); } } catch (Exception ex) { _log.Error($"{FtpTemplateMessage("DownloadLocal", Filename, "throw exception")}", ex); throw ex; } }
/// <summary> /// Create a directory under the GameRootDirectory location /// </summary> /// <param name="gameName">Name of directory to be created</param> public void CreateGame(string gameName) { ValidateFtpClient(); _ftpClient.SetWorkingDirectory(_ftpXboxSettings.GameRootDirectory); string gameroot = Path.Combine(_ftpXboxSettings.GameRootDirectory, gameName); _ftpClient.CreateDirectory(gameroot); _ftpClient.SetWorkingDirectory(gameroot); }
public void SFTP_shouldCreateDirectory() { //Arrange ReMoveDirectoryIfExist(FromIntrum); // Act _ftp.CreateDirectory(FromIntrum + DirectoryName); var SUT = _ftp.DirectoryListSimple(FromIntrum); // Assert SUT.Should().NotBeNullOrEmpty(); SUT.Should().Contain(DirectoryName); }
private void CreateSubDir_Click(object sender, EventArgs e) { TreeNode pNode = this.SelectedNode; int ID = this.GetNodeID(pNode); string ParentPath = this.GetFtpPathByNode(pNode); string sql = "select SEQ_UP_DIRS.nextval from dual"; IDataBase pDataBase = SysDBConfig.GetInstance().GetOleDataBase("OrclConn"); pDataBase.OpenConnection(); string DirID = pDataBase.ExecuteScalar(sql).ToString(); string Path = string.Format("{0}/{1}", this.GetFtpPathByNode(pNode), DirID); sql = string.Format("insert into up_dirs values({0},'新建目录',{1},'{2}\\{0}',1)", DirID, ID, ParentPath); pDataBase.ExecuteNonQuery(sql); pDataBase.CloseConnection(); this.RefreshSubTreeView(pNode); pNode.ExpandAll(); TreeNode NewNode = this.FindNodeByID(int.Parse(DirID)); IFtpClient pFtpClient = SysDBConfig.GetInstance().GetFtpClient("CHXQ"); pFtpClient.Connect(); string FtpPath = GetFtpPathByNode(NewNode); pFtpClient.CreateDirectory(FtpPath); pFtpClient.Close(); this.SelectedNode = NewNode; this.LabelEdit = true; NewNode.BeginEdit(); }
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}"); } } } }
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(); } }
protected override void FileAction(IFtpClient client) { foreach (var item in Items) { if (item.Item.Type == FileSystemItemType.File) { UploadFile(client, (ILocalFile)item.Item, item.DestinationPath, item.OverWrite).Wait(); } else { var folder = (ILocalDirectory)item.Item; var newDirectoryPath = item.DestinationPath + "/" + folder.Name; client.CreateDirectory(newDirectoryPath); UploadDirectory(client, folder, newDirectoryPath).Wait(); } } }
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}"); } } }
private Task UploadDirectory(IFtpClient client, ILocalDirectory storagefolder, string remoteDirectoryPath) { return(Task.Run(() => { return storagefolder.GetFilesAsync().Result; }) .ContinueWith(fileTask => { var files = fileTask.Result; if (files != null && files.Any()) { foreach (var file in files) { UploadFile(client, file, remoteDirectoryPath, false).Wait(); } } }).ContinueWith(folderTask => { return storagefolder.GetFoldersAsync().Result; }) .ContinueWith(folderTask => { var folders = folderTask.Result; if (folders == null || !folders.Any()) { return; } foreach (var folder in folders) { var newDirectoryPath = remoteDirectoryPath + "/" + folder.Name; client.CreateDirectory(newDirectoryPath); UploadDirectory(client, folder, newDirectoryPath).Wait(); } })); }