Ftp.MakeDirectory 嵌套结构

发布于 2025-01-02 02:58:24 字数 476 浏览 0 评论 0原文

如果你们没有在 ftp 上创建子目录的权限,你们会怎么做? 显然,您无法在一个命令中创建目录

,因此:

path = "ftp://someftp.com/files/newFolder/innerFolder";
var request = (FtpWebRequest)WebRequest.Create(path);
request.Method = WebRequestMethods.Ftp.MakeDirectory;
var response = (FtpWebResponse)request.GetResponse());

如果“files”或“newFolder”已存在,则会抛出错误代码为 550 的异常

  1. 如何检查我是否有权创建子目录?< /p>

  2. 如果我没有这些权利我该怎么办?请向我展示可让您在多个命令中创建文件夹结构的代码。

What do you guys do if you lack of permissions to create subdirs on a ftp?
Apparently you can't create a directory in one command

So this:

path = "ftp://someftp.com/files/newFolder/innerFolder";
var request = (FtpWebRequest)WebRequest.Create(path);
request.Method = WebRequestMethods.Ftp.MakeDirectory;
var response = (FtpWebResponse)request.GetResponse());

will throw an exception with error code 550 if 'files' or 'newFolder' is already exists

  1. How can I check if I have the rights to create subdirs?

  2. What can I do if I don't have those rights? Show me please code that lets you create a folder structure in more than one command.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

↘人皮目录ツ 2025-01-09 02:58:24

好吧:因为 FtpWebRequest 是无状态的 - 无法更改 FTP 上的当前目录。幸运的是,我们可以使用开源 FTP 库之一。以下是 AlexPilotti 的 FTPS 库的示例,可通过 NuGet 获取

using (var client = new FTPSClient())
{
     var address = Regex.Match(path, @"^(ftp://)?(\w*|.?)*/").Value.Replace("ftp://", "").Replace("/", "");
     var dirs = Regex.Split(path.Replace(address, "").Replace("ftp://", ""), "/").Where(x => x.Length > 0);
     client.Connect(address, credential, ESSLSupportMode.ClearText);
     foreach (var dir in dirs)
     {
        try
           {
              client.MakeDir(dir);
           }
        catch (FTPException)
        {
        }
        client.SetCurrentDirectory(dir);
     }

   }
}

Alright: because FtpWebRequest is stateless - there is no way to change current directory on FTP. Fortunately we can use one of the open-source FTP libraries. Here's an example with AlexPilotti's FTPS library, which is availible through NuGet

using (var client = new FTPSClient())
{
     var address = Regex.Match(path, @"^(ftp://)?(\w*|.?)*/").Value.Replace("ftp://", "").Replace("/", "");
     var dirs = Regex.Split(path.Replace(address, "").Replace("ftp://", ""), "/").Where(x => x.Length > 0);
     client.Connect(address, credential, ESSLSupportMode.ClearText);
     foreach (var dir in dirs)
     {
        try
           {
              client.MakeDir(dir);
           }
        catch (FTPException)
        {
        }
        client.SetCurrentDirectory(dir);
     }

   }
}
靑春怀旧 2025-01-09 02:58:24

你是对的。您无法使用一条命令创建 FTP 的完整路径。最简单的方法可能如下:

  1. 创建一个接受 Uri 的 make 目录方法。
  2. 确保您可以区分部分路径不存在的错误和其他错误。
  3. 如果不是路径错误,则重新抛出异常。
  4. 如果是路径错误:
  5. 修剪路径中的最后一个目录。
  6. 如果没有什么可以通过抛出适当的新异常来修剪退出。
  7. 尝试创建新路径。
  8. 然后尝试再次创建最终目录。

因此本质上,它在两个限制条件下变得递归,发生非路径异常并且没有任何东西可以修剪。

You are correct. You cannot create an entire path in one command to FTP. The simplest way to do is probably as follows:

  1. Create a make directory method that accepts the Uri.
  2. Make sure that you can differentiate between errors where part of the path doesn't exist and other errors.
  3. If it's anything but a path error, rethrow the exception.
  4. If it's a path error:
  5. Trim the last directory from the path.
  6. If there's nothing more to trim quit by throwing an appropriate new exception.
  7. Try to create the new path.
  8. Then try to create the final directory again.

So essentially, it becomes recursive with two limit conditions, non-path exceptions occurring and nothing left to trim.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文