C#
FTP
programming
directory-creation
file-transfer

How do I create a directory on FTP server using C?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Creating a directory on an FTP server from C# is a request/response operation: you send a MakeDirectory command to a specific FTP URL, and the server decides whether it succeeds. The practical work is in choosing the right protocol, constructing the path correctly, and handling the fact that many FTP servers do not create intermediate folders automatically.

Use MakeDirectory Against the Target Folder URL

For classic FTP or FTPS, the built-in API is FtpWebRequest. The important point is that the request URL should already include the directory you want to create.

csharp
1using System;
2using System.Net;
3
4public class Program
5{
6    public static void Main()
7    {
8        string ftpUrl = "ftp://example.com/incoming/reports";
9        string username = "user";
10        string password = "pass";
11
12        var request = (FtpWebRequest)WebRequest.Create(ftpUrl);
13        request.Method = WebRequestMethods.Ftp.MakeDirectory;
14        request.Credentials = new NetworkCredential(username, password);
15        request.UsePassive = true;
16        request.KeepAlive = false;
17
18        using var response = (FtpWebResponse)request.GetResponse();
19        Console.WriteLine(response.StatusDescription.Trim());
20    }
21}

There is no separate property that says "create a folder named X under this parent." If the URL is wrong, the server will create the wrong directory or reject the request.

Capture Real FTP Errors

When directory creation fails, the useful information is usually in the FtpWebResponse attached to the WebException. You want that status because "already exists" and "permission denied" should not be handled the same way.

csharp
1using System;
2using System.Net;
3
4public static class FtpHelpers
5{
6    public static void CreateDirectory(string ftpUrl, NetworkCredential credentials)
7    {
8        try
9        {
10            var request = (FtpWebRequest)WebRequest.Create(ftpUrl);
11            request.Method = WebRequestMethods.Ftp.MakeDirectory;
12            request.Credentials = credentials;
13            request.UsePassive = true;
14            request.KeepAlive = false;
15
16            using var response = (FtpWebResponse)request.GetResponse();
17            Console.WriteLine($"Created: {response.StatusDescription.Trim()}");
18        }
19        catch (WebException ex) when (ex.Response is FtpWebResponse ftpResponse)
20        {
21            Console.WriteLine($"FTP failure: {ftpResponse.StatusCode} {ftpResponse.StatusDescription.Trim()}");
22        }
23    }
24}

In production code, you would usually translate those server responses into application-specific outcomes rather than writing them directly to the console.

Build Nested Paths One Segment at a Time

Many FTP servers will not create a/b/c in one request if a or b does not already exist. A reliable pattern is to create each segment in order.

csharp
1using System;
2using System.Net;
3
4public static class FtpDirectoryTree
5{
6    public static void EnsurePath(string baseUrl, NetworkCredential credentials, params string[] segments)
7    {
8        string current = baseUrl.TrimEnd('/');
9
10        foreach (string segment in segments)
11        {
12            current += "/" + segment;
13            FtpHelpers.CreateDirectory(current, credentials);
14        }
15    }
16}

That approach is simple and portable across FTP servers with different behaviors.

It also makes error handling clearer because you know exactly which path segment failed instead of receiving one generic failure for the whole nested path.

Distinguish FTP, FTPS, and SFTP

This is a common source of confusion. FtpWebRequest supports FTP and FTPS. It does not support SFTP because SFTP is an SSH-based protocol, not an FTP variant.

If the server requires FTPS, enable TLS:

csharp
request.EnableSsl = true;

If the server documentation says SFTP, switch tools entirely and use an SSH-capable library such as SSH.NET. Do not keep changing FTP settings and expect them to work against an SFTP endpoint.

Consider the Runtime and Library Choice

FtpWebRequest is common in legacy .NET and older enterprise applications. In new code, some teams prefer dedicated FTP libraries because they offer cleaner async support and more convenient higher-level APIs. The underlying protocol concepts remain the same either way: authenticate correctly, target the correct path, and read server responses rather than guessing.

So the protocol decision comes first, and the library decision comes second. If the target server is really SFTP, the cleanest FtpWebRequest code in the world will still fail.

Common Pitfalls

  • Using FtpWebRequest against an SFTP server.
  • Sending the request to the wrong URL and creating the folder in the wrong place.
  • Assuming nested directories will be created automatically.
  • Ignoring the server response when a request fails.
  • Forgetting FTPS settings when the server requires TLS.

Summary

  • Use WebRequestMethods.Ftp.MakeDirectory with the full target folder URL.
  • Inspect FtpWebResponse details when the server rejects the request.
  • Create nested paths one segment at a time for better compatibility.
  • Distinguish FTP and FTPS from SFTP before choosing the client API.
  • Treat path construction and protocol selection as the first debugging steps.

Course illustration
Course illustration

All Rights Reserved.