Ask Your Question

Revision history [back]

Here are the steps to establish a directory on an FTP server using C#:

  1. Create an instance of the FtpWebRequest class and specify the server URL.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/new_directory");
  1. Set the request method to "MKD" (make directory).
request.Method = WebRequestMethods.Ftp.MakeDirectory;
  1. Set the FTP credentials.
request.Credentials = new NetworkCredential("username", "password");
  1. Send the request and get the FTP server response.
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
  1. Get the status code from the response.
Console.WriteLine("Directory created: {0}", response.StatusDescription);
  1. Close the response.
response.Close();

The complete code:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/new_directory");
request.Method = WebRequestMethods.Ftp.MakeDirectory;
request.Credentials = new NetworkCredential("username", "password");

FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Directory created: {0}", response.StatusDescription);

response.Close();