0

This is the path I am trying to upload to the ftp server:

_ftp://ftp-server/products/productxx/versionxx/releasexx/delivery/data.zip

The problem is that the folders "productxx/versionxx/releasexx/delivery/" do not exist on the server.
Can I create them automatically while uploading the .zip file in c#

My coding at the moment is:

                FtpWebRequest request =
                    (FtpWebRequest)WebRequest.Create(pathToFtp);
                // Method set to UploadFile 
                request.Method = WebRequestMethods.Ftp.UploadFile;
                // set password and username
                request.Credentials = new NetworkCredential(UserName, Password);
                // write MemoryStream in ftpStream 
                using (Stream ftpStream = request.GetRequestStream())
                {
                    memoryStream.CopyTo(ftpStream);
                }

I am getting the System.Net.WebException: "Can't connect to FTP: (553) File name not allowed" at "using (Stream ftpStream =request.GetRequestStream())"

but if my pathToFtp is _ftp://ftp-server/products/data.zip it´s working well

Xcodian Solangi
  • 2,342
  • 5
  • 24
  • 52
ARX
  • 287
  • 1
  • 5
  • 11

1 Answers1

1

One of the request methods available is WebRequestMethods.Ftp.MakeDirectory. You should be able to use that to do what you want.

Something like this (though I've not tested it), should do the trick:

async Task CreateDirectory(string path)
{
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(path);
    request.Method = WebRequestMethods.Ftp.MakeDirectory;

    using (var response = (FtpWebResponse)(await request.GetResponseAsync()))
    {
        Console.WriteLine($"Created: {path}");
    }
}

It's answered in more detail here How do I create a directory on ftp server using C#?

Callum Evans
  • 331
  • 3
  • 18
  • thank you, it not give a method witch create the directory directly if is not exist iam right? – ARX Oct 13 '17 at 11:55
  • 1
    There are `WebRequestMethods.Ftp.ListDirectory` and `WebRequestMethods.Ftp.ListDirectoryDetails` request methods which could help you with that. You could just try to create the directory regardless and handle the response. As far as I'm aware there isn't a "create if doesn't exist" command. – Callum Evans Oct 13 '17 at 12:13
  • What the point of using `await request.GetResponseAsync()`? Use `request.GetResponse()` directly. – Martin Prikryl Oct 13 '17 at 12:34