如何使用C#在FTP服务器上创建目录?

如何使用C#在FTP服务器上创建目录?,c#,.net,ftp,webclient,C#,.net,Ftp,Webclient,使用C#在FTP服务器上创建目录的简单方法是什么 我找到了如何将文件上载到现有文件夹的方法,如下所示: using (WebClient webClient = new WebClient()) { string filePath = "d:/users/abrien/file.txt"; webClient.UploadFile("ftp://10.128.101.78/users/file.txt", filePath); } // remoteUri points out

使用C#在FTP服务器上创建目录的简单方法是什么

我找到了如何将文件上载到现有文件夹的方法,如下所示:

using (WebClient webClient = new WebClient())
{
    string filePath = "d:/users/abrien/file.txt";
    webClient.UploadFile("ftp://10.128.101.78/users/file.txt", filePath);
}
// remoteUri points out an ftp address ("ftp://server/thefoldertocreate")
WebRequest request = WebRequest.Create(remoteUri);
request.Method = WebRequestMethods.Ftp.MakeDirectory;
WebResponse response = request.GetResponse();

但是,如果我想上传到
users/abrien
,我会得到一个
WebException
文件不可用。我假设这是因为我需要在上传文件之前创建新文件夹,但是
WebClient
似乎没有任何方法来完成这一点。

使用
FtpWebRequest
,方法为

例如:

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);
        }
    }
}
大概是这样的:

using (WebClient webClient = new WebClient())
{
    string filePath = "d:/users/abrien/file.txt";
    webClient.UploadFile("ftp://10.128.101.78/users/file.txt", filePath);
}
// remoteUri points out an ftp address ("ftp://server/thefoldertocreate")
WebRequest request = WebRequest.Create(remoteUri);
request.Method = WebRequestMethods.Ftp.MakeDirectory;
WebResponse response = request.GetResponse();

(有点晚了。真奇怪。)

如果您想创建嵌套目录,下面是答案

没有干净的方法来检查ftp上是否存在文件夹,因此您必须循环并创建所有嵌套结构,每次创建一个文件夹

public static void MakeFTPDir(string ftpAddress, string pathToCreate, string login, string password, byte[] fileContents, string ftpProxy = null)
    {
        FtpWebRequest reqFTP = null;
        Stream ftpStream = null;

        string[] subDirs = pathToCreate.Split('/');

        string currentDir = string.Format("ftp://{0}", ftpAddress);

        foreach (string subDir in subDirs)
        {
            try
            {
                currentDir = currentDir + "/" + subDir;
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(currentDir);
                reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential(login, password);
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                ftpStream = response.GetResponseStream();
                ftpStream.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                //directory already exist I know that is weak but there is no way to check if a folder exist on ftp...
            }
        }
    }

创建FTP目录可能很复杂,因为您必须检查目标文件夹是否存在。您可能需要使用FTP库来检查和创建目录。您可以看看这个例子:

关于如何通过HTTP代理实现这一点,您有什么想法吗?(FtpWebRequest不支持)恐怕不是真的。根据我在HTTP代理上工作的记忆,他们将HTTP方法转换为FTP命令——我想不出一种等效的HTTP方法:(可以用一个WebRequest创建嵌套目录吗?我正在尝试创建“”,但我得到了“WebException-550”(找不到文件,无法访问)不知道这是不是原因。对此的预期(成功)响应是什么?文档似乎没有涵盖它。尝试创建一个已经存在的目录会给ma一个
550
,如上所述(除了引发异常)最后两个参数在做什么?在方法体中没有使用这些参数。我使用了没有最后两个参数的方法,但只能创建高达2级的嵌套目录,之后我得到了505个错误。它返回的错误如下:远程服务器返回错误:(550)文件不可用(例如,找不到文件,无法访问).Plz Help Me.@GhanshyamLakhani在我看来,该文件不可访问。您检查过目录权限吗?无法解释这对我有多重要。谢谢!仅供参考,假设您使用的是较新的C#版本,您可以执行以下操作:捕获(WebException ex)时(例如,响应为FtpWebResponse ftpResponse&&ftpResponse.StatusDescription.Contains((“文件已存在”))