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.
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.
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.
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.
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); } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With