【问题标题】:How do I create a directory on FTP server using C#?如何使用 C# 在 FTP 服务器上创建目录?
【发布时间】:2010-10-26 00:41:02
【问题描述】:

使用 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);
}

但是,如果我想上传到 users/abrien,我会收到 WebException 说文件不可用。我认为这是因为我需要在上传文件之前创建新文件夹,但WebClient 似乎没有任何方法可以实现。

【问题讨论】:

    标签: c# .net ftp webclient


    【解决方案1】:

    创建 FTP 目录可能很复杂,因为您必须检查目标文件夹是否存在。您可能需要使用 FTP 库来检查和创建目录。你可以看看这个:http://www.componentpro.com/ftp.net/ 和这个例子:http://www.componentpro.com/doc/ftp/Creating-a-new-directory-Synchronously.htm

    【讨论】:

      【解决方案2】:

      如果你想创建嵌套目录,这里是答案

      没有干净的方法来检查 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...
                  }
              }
          }
      

      【讨论】:

      • 最后两个参数在做什么?这些没有在方法体中使用。我使用没有最后两个参数的方法,但只能设法创建最多2级的嵌套目录,之后出现505错误。
      • @GhanshyamLakhani 在我看来该文件无法访问。你检查目录权限了吗?
      • 无法解释这对我来说有多重要。赞一个!
      • 仅供参考,假设您使用的是较新的 C# 版本,您可以这样做:catch (WebException ex) when (ex.Response is FtpWebResponse ftpResponse && ftpResponse.StatusDescription.Contains("File exists"))
      • @KyleP:检查魔术字符串是危险的,因为它取决于服务器和本地化。最好使用 ftpResponse.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable 代码 550
      【解决方案3】:

      类似这样的:

      // remoteUri points out an ftp address ("ftp://server/thefoldertocreate")
      WebRequest request = WebRequest.Create(remoteUri);
      request.Method = WebRequestMethods.Ftp.MakeDirectory;
      WebResponse response = request.GetResponse();
      

      (有点晚了。真奇怪。)

      【讨论】:

        【解决方案4】:

        使用FtpWebRequest,方法为WebRequestMethods.Ftp.MakeDirectory

        例如:

        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);
                }
            }
        }
        

        【讨论】:

        • 关于如何通过 HTTP 代理执行此操作的任何想法? (FtpWebRequest 不支持)
        • 恐怕不是。根据我对 HTTP 代理工作的记忆,他们将 HTTP 方法转换为 FTP 命令——我想不出等效的 HTTP 方法:(
        • 一个WebRequest可以创建嵌套目录吗?我正在尝试制作“host.com/ExistingFolder/new1/new2”,但我收到“WebException - 550”(找不到文件,无法访问)并且不知道天气原因。
        • 对此的预期(成功)响应是什么?该文档似乎没有涵盖它。尝试创建一个已经存在的目录是给 ma 一个550 如上所述(除了抛出异常)
        猜你喜欢
        • 1970-01-01
        • 2013-01-14
        • 2018-03-25
        • 2015-08-18
        • 2018-10-22
        • 2012-06-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多