public static void EnsureParentDirectoryExists(ISftpClient connection, string remotePath) { var pos = remotePath.LastIndexOf('/'); if (pos > 0) { var dir = remotePath.Substring(0, pos); if (!connection.DirectoryExists(dir)) { CreateDirectoryTree(connection, "", dir.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries)); } } }