private async Task MoveAsync(string path, string fileName, string newPathAndFileName) { var oldPathAndFileName = Path.Combine(path, fileName).Replace('\\', '/'); var newFullPath = newPathAndFileName.Replace('\\', '/'); using (var ftpClient = new Ftp()) { ftpClient.Passive = _passive; ftpClient.TransferType = _transferType; await ftpClient.ConnectAsync(_serverAddress, _port, SslMode.None); await ftpClient.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 ftpClient.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 ftpClient.DirectoryExistsAsync(dirString)) { await ftpClient.CreateDirectoryAsync(dirString); } } } if (await ftpClient.FileExistsAsync(newFullPath)) { await ftpClient.DeleteFileAsync(newFullPath); } await ftpClient.RenameAsync(oldPathAndFileName, newFullPath); await ftpClient.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 ftpClient = new Ftp()) { ftpClient.Passive = _passive; ftpClient.TransferType = _transferType; // Connect & authenticate: await ftpClient.ConnectAsync(_serverAddress, _port, SslMode.None); await ftpClient.LoginAsync(_userName, _password); // If the directory does not exist, create it if allowed: if (!await ftpClient.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 ftpClient.DirectoryExistsAsync(dirString)) { await ftpClient.CreateDirectoryAsync(dirString); } } } else { throw new Exception($"Directory '{path}' was not found."); } } // Overwrite existing files if allowed: if (await ftpClient.FileExistsAsync(destinationFilePath)) { if (overwriteIfExists) { await ftpClient.DeleteFileAsync(destinationFilePath); } else { throw new Exception($"File '{destinationFilePath}' already exists."); } } // Upload the file with a temporary file name: await ftpClient.PutFileAsync(messageStream, tempFilePath); await ftpClient.RenameAsync(tempFilePath, destinationFilePath); await ftpClient.DisconnectAsync(); } return(tempFileName); }