/// <summary> /// Creates a directory, creating all required intermediate directories. /// Ok if the directory already exists (idempotent) like <see cref="CreateDirectoryOkIfExists(SftpClient, string)"/>. /// </summary> /// <remarks> /// The <see cref="Renci.SshNet.SftpClient.CreateDirectory(string)"/> method will not make intermediate directories for nested directories. For example, if A exists, but B does not, a call to create C such that /A/B/C will fail with exception: /// Renci.SshNet.Common.SftpPathNotFoundException: 'No such file' /// </remarks> public static void CreateDirectoryOkIfIntermediatesAndExists(this SftpClient sftpClient, string directoryPath) { // The SftpClient.CreateDirectory() call will not make intermediate directories as required for a path. // This work-around walks up the directory tree until a parent directory exists, and makes intermediate directories as required. var parentDirectoryPath = PathUtilities.GetParentDirectoryPath(directoryPath); if (!sftpClient.Exists(parentDirectoryPath)) { sftpClient.CreateDirectoryOkIfIntermediatesAndExists(parentDirectoryPath); } sftpClient.CreateDirectoryOkIfExists(directoryPath); }