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.
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.
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.
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:
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
FtpWebRequestagainst 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.MakeDirectorywith the full target folder URL. - Inspect
FtpWebResponsedetails 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.

