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.
Here is the answer if you want to create nested directories
There is no clean way to check if a folder exist on the ftp so you have to loop and create all the nested structure one folder at the time
Creating an FTP directory might be complicated since you have to check if the destination folder exists or not. You may need to use an FTP library to check and create a directory. You can take a look at this one: http://www.componentpro.com/ftp.net/ and this example: http://www.componentpro.com/doc/ftp/Creating-a-new-directory-Synchronously.htm
Something like this:
(a bit late. how odd.)
Use
FtpWebRequest
, with a method ofWebRequestMethods.Ftp.MakeDirectory
.For example: