Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

What's an easy way to create a directory on an FTP server using C#?

I figured out how to upload a file to an already existing folder like this:

using (WebClient webClient = new WebClient()) {     string filePath = "d:/users/abrien/file.txt";     webClient.UploadFile("ftp://10.128.101.78/users/file.txt", filePath); } 

However, if I want to upload to users/abrien, I get a WebException saying the file is unavailable. I assume this is because I need to create the new folder before uploading my file, but WebClient doesn't seem to have any methods to accomplish that.

like image 867
Anthony Brien Avatar asked May 13 '09 21:05

Anthony Brien


People also ask

Can FTP create directories?

The MKdir subcommand directs the FTP client to send an MKD command to the remote host FTP server to create a directory with name directory. If the server is a Communications Server for z/OS FTP server, and directory is a fully qualified MVS data set name, the server allocates a PDS or PDSE named directory.

Where is the FTP directory?

In the Connections pane, expand the server name, and then go to the site, application, or directory where you want to configure directory browsing. In the Home pane, double-click FTP Directory Browsing.

How do I change the directory on an FTP server?

To change the working directory or subdirectory: On the File menu, click Change Directory. In the Name box, type the new path name. Click OK.


1 Answers

Use FtpWebRequest, with a method of WebRequestMethods.Ftp.MakeDirectory.

For example:

using System; using System.Net;  class Test {     static void Main()     {         WebRequest request = WebRequest.Create("ftp://host.com/directory");         request.Method = WebRequestMethods.Ftp.MakeDirectory;         request.Credentials = new NetworkCredential("user", "pass");         using (var resp = (FtpWebResponse) request.GetResponse())         {             Console.WriteLine(resp.StatusCode);         }     } } 
like image 164
Jon Skeet Avatar answered Oct 09 '22 03:10

Jon Skeet