private async Task MoveAsync(string path, string fileName, string newPathAndFileName) { var oldPathAndFileName = Path.Combine(path, fileName).Replace('\\', '/'); var newFullPath = newPathAndFileName.Replace('\\', '/'); using (var sftpClient = new Sftp()) { sftpClient.TransferType = _transferType; await sftpClient.ConnectAsync(_serverAddress, _port); await sftpClient.LoginAsync(_userName, _password); var dir = Path.GetDirectoryName(newFullPath)?.Replace('\\', '/'); // If the directory hasn't been specified, use user home directory if (!string.IsNullOrEmpty(dir) && !await sftpClient.DirectoryExistsAsync(dir)) { // Examine the path step by step and create directories: StringBuilder directory = null; foreach (var directoryPart in dir.Split('/')) { // First directory should not be preceded by '/': if (directory == null) { directory = new StringBuilder(); directory.Append(directoryPart); } else { directory.AppendFormat("/{0}", directoryPart); } // If this directory does not exist, create it and move to the next part: var dirString = directory.ToString(); if (!string.IsNullOrWhiteSpace(dirString) && !await sftpClient.DirectoryExistsAsync(dirString)) { await sftpClient.CreateDirectoryAsync(dirString); } } } if (await sftpClient.FileExistsAsync(newFullPath)) { await sftpClient.DeleteFileAsync(newFullPath); } await sftpClient.RenameAsync(oldPathAndFileName, newFullPath); await sftpClient.DisconnectAsync(); } }
public async Task <string> WriteFileAsync(string path, string fileName, Stream messageStream, bool overwriteIfExists = false, bool createDirectory = false) { var tempFileName = $".{Guid.NewGuid()}.tmp"; var destinationFilePath = Path.Combine(path, FileNameHelper.PopulateFileNameMacros(fileName)).Replace('\\', '/'); var tempFilePath = Path.Combine(path, tempFileName).Replace('\\', '/'); path = path.Replace('\\', '/'); using (var sftpClient = new Sftp()) { sftpClient.TransferType = _transferType; // Connect & authenticate: await sftpClient.ConnectAsync(_serverAddress, _port); await sftpClient.LoginAsync(_userName, _password); // If the directory does not exist, create it if allowed: if (!await sftpClient.DirectoryExistsAsync(path)) { if (createDirectory) { // Examine the path step by step and create directories: StringBuilder directory = null; foreach (var directoryPart in path.Split('/')) { // First directory should not be preceded by '/': if (directory == null) { directory = new StringBuilder(); directory.Append(directoryPart); } else { directory.AppendFormat("/{0}", directoryPart); } // If this directory does not exist, create it and move to the next part: var dirString = directory.ToString(); if (!string.IsNullOrWhiteSpace(dirString) && !await sftpClient.DirectoryExistsAsync(dirString)) { await sftpClient.CreateDirectoryAsync(dirString); } } } else { throw new Exception($"Directory '{path}' was not found."); } } // Overwrite existing files if allowed: if (await sftpClient.FileExistsAsync(destinationFilePath)) { if (overwriteIfExists) { await sftpClient.DeleteFileAsync(destinationFilePath); } else { throw new Exception($"File '{destinationFilePath}' already exists."); } } // Upload the file with a temporary file name: await sftpClient.PutFileAsync(messageStream, tempFilePath); await sftpClient.RenameAsync(tempFilePath, destinationFilePath); await sftpClient.DisconnectAsync(); } return(tempFileName); }