C# 在将文件上载到ftp之前,如何在ftp服务器上创建目录?

C# 在将文件上载到ftp之前,如何在ftp服务器上创建目录?,c#,.net,winforms,ftp,C#,.net,Winforms,Ftp,这是一个将文本文件上传到我的ftp根目录的工作代码。测试和工作。 但是现在我想在根目录上创建一个子目录,然后在创建目录后上传文件 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Net; namespace mws { class FtpFile

这是一个将文本文件上传到我的ftp根目录的工作代码。测试和工作。 但是现在我想在根目录上创建一个子目录,然后在创建目录后上传文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;

namespace mws
{
    class FtpFileUpload
    {
        static string ftpurl = "ftp://ftp.newsxpressmedia.com/";
        static string filename = @"c:\temp\FtpTestFile.txt";
        static string ftpusername = "Username";
        static string ftppassword = "Password";
        static string ftpdirectory = "subtestdir";

        public static void test()
        {
            try
            {
                FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(
                ftpurl + "/" + Path.GetFileName(filename));
                request.Method = WebRequestMethods.Ftp.UploadFile;
                request.Credentials = new NetworkCredential(ftpusername, ftppassword);
                StreamReader sourceStream = new StreamReader(@"c:\temp\test1.txt");
                byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
                sourceStream.Close();
                request.ContentLength = fileContents.Length;
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(fileContents, 0, fileContents.Length);
                requestStream.Close();
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);

                response.Close();
            }
            catch (Exception err)
            {
                string t = err.ToString();
            }
        }
    }
}
我尝试更改第一行,并添加了一行以创建目录:

FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(
                ftpurl + "/" + ftpdirectory + "/" + Path.GetFileName(filename));
                request.Method = WebRequestMethods.Ftp.MakeDirectory;
但是我得到了异常错误:

远程服务器返回错误:(550)文件不可用(例如,找不到文件,无法访问)

但在改变之前,它运行良好,没有问题。
如何首先在服务器上创建一个目录,然后将文件上载到创建的目录中?

您是否检查了以下答案:

看起来您为
MakeDirectory
请求提供的路径实际上是一个文件名,而不是目录名。

可能重复的路径必须发出两个请求。正如我提到的问题的答案所建议的那样,你不能在一个请求中做到这一点。所以你首先创建目录,然后上传文件。最后我使用了这个代码项目代码:我使用我自己的文件上传方法,还添加了一些更改和选项,现在它工作得很好。
public static void CreateDirectoryandSaveImage(string username, string password)
    {
       string ftphost = "ftp.placemyorder.com";
        string filePath = @"D:\ddlState.png";
        //You could not use "ftp://" + ftphost + "/FTPUPLOAD/WPF/WPF.txt" to create the folder,this will result the 550 error.
        string ftpfullpath = "ftp://" + ftphost + "/TestFolder";
    //    //Create the folder, Please notice if the WPF folder already exist, it will result 550 error.
        FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
        ftp.Credentials = new NetworkCredential("ftp.placemyorder.com|placemyorder", "Cabbage123");
        ftp.UsePassive = true;
        ftp.UseBinary = true;
        ftp.KeepAlive = false;
        ftp.Method = WebRequestMethods.Ftp.MakeDirectory;
       FtpWebResponse CreateForderResponse = (FtpWebResponse)ftp.GetResponse();
        if (CreateForderResponse.StatusCode == FtpStatusCode.PathnameCreated)
        {
           //If folder created, upload file
            var fileName = Path.GetFileName(filePath);
            string ftpUploadFullPath = "ftp://" + ftphost + "/TestFolder/" + fileName;
            FtpWebRequest ftpUpLoadFile = (FtpWebRequest)FtpWebRequest.Create(ftpUploadFullPath);
            ftpUpLoadFile.KeepAlive = true;
           ftpUpLoadFile.UseBinary = true;
          ftpUpLoadFile.Method = WebRequestMethods.Ftp.UploadFile;
            ftpUpLoadFile.Credentials = new NetworkCredential(username, password);
            ftpUpLoadFile.UsePassive = true;
            ftpUpLoadFile.UseBinary = true;
            ftpUpLoadFile.KeepAlive = false;
           FileStream fs = File.OpenRead(filePath);
            byte[] buffer = new byte[fs.Length];
           fs.Read(buffer, 0, buffer.Length);
           fs.Close();
           Stream ftpstream = ftpUpLoadFile.GetRequestStream();
          ftpstream.Write(buffer, 0, buffer.Length);
           ftpstream.Close();
      }

    }