How to create a directory on an FTP server using C#?

Ok, so I already can upload a file to an FTP server: How to upload a file to an FTP server using C#?

However, now I need to create a directory first.

It follows some basic steps:

  1. Open a request using the full destination ftp path: Ftp://Ftp.Server.tld/ or Ftp://Ftp.Server.tld/Some/Path
  2. Configure the connection request
  3. Call GetResponse() method to actually attempt to create the directory
  4. Verify that it worked.

See the steps inside the source as comments:

using System;
using System.IO;
using System.Net;

namespace CreateDirectoryOnFtpServer
{
    class Program
    {
        static void Main(string[] args)
        {
            CreateDirectoryOnFTP("ftp://ftp.server.tld", /*user*/"User1", /*pw*/"Passwd!", "NewDirectory");

        }

        static void CreateDirectoryOnFTP(String inFTPServerAndPath, String inUsername, String inPassword, String inNewDirectory)
        {
            // Step 1 - Open a request using the full URI, ftp://ftp.server.tld/path/file.ext
            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(inFTPServerAndPath + "/" + inNewDirectory);

            // Step 2 - Configure the connection request
            request.Credentials = new NetworkCredential(inUsername, inPassword);
            request.UsePassive = true;
            request.UseBinary = true;
            request.KeepAlive = false;

            request.Method = WebRequestMethods.Ftp.MakeDirectory;

            // Step 3 - Call GetResponse() method to actually attempt to create the directory
            FtpWebResponse makeDirectoryResponse = (FtpWebResponse)request.GetResponse();
        }
    }
}

All right, now you have created a directory on the FTP server.


Copyright ® Rhyous.com – Linking to this page is allowed without permission and as many as ten lines of this page can be used along with this link. Any other use of this page is allowed only by permission of Rhyous.com.

4 Comments

  1. hendo says:

    I keep getting a Remote server returned an error: (550) File unavailable (not found, no access) using:

    FtpWebRequest req = (FtpWebRequest)WebRequest.Create(custPath);
    req.Credentials = new NetworkCredential("username", "password");
    req.UsePassive = true;
    req.UseBinary = true;
    req.KeepAlive = false;
    req.Method = WebRequestMethods.Ftp.MakeDirectory;
    FtpWebResponse makeDirectoryResponse = (FtpWebResponse)req.GetResponse();

    I need to make a series of folders on the ftp server and then send up files. Making the folders just isn't working for me.

  2. fernando says:

    Gracias, muy bueno. Thank You very much!!

  3. Nice Work... It's Verry Help Ful..
    Tanx for share this topic.

  4. Salman Elahi says:

    Hi,

    I was having trouble using this code, it works for the first time but if you are trying to upload
    multiple files and you need to check the existence of directory more than once it was not working. However, with a slight change this code works for both cases, where directory is the full ftp path
    i.e. ftp://my.ftp.com/root/newDirectory

    private static bool FtpDirectoryExists(string directory, string username, string password)
    {

    try
    {
    var request = (FtpWebRequest)WebRequest.Create(directory);
    request.Credentials = new NetworkCredential(username, password);
    request.Method = WebRequestMethods.Ftp.ListDirectory;

    FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    }
    catch (WebException ex)
    {
    FtpWebResponse response = (FtpWebResponse)ex.Response;
    if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
    return false;
    else
    return true;
    }
    return true;

    }

Leave a Reply

How to post code in comments?