如何使用 FtpWebRequest (.NET) 更改目录?

How do you change directories using FtpWebRequest (.NET)?(如何使用 FtpWebRequest (.NET) 更改目录?)
本文介绍了如何使用 FtpWebRequest (.NET) 更改目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

谁能告诉我如何使用 FtpWebRequest 更改目录?这似乎应该是一件容易的事,但我没有看到它.

编辑

我只是想补充一下...我对 FtpWebRequest 没有兴趣.如果有更好(更简单)的方式在 .NET 中执行 FTP,请告诉我.

<小时>

显然没有办法使用实时连接来做到这一点,您需要更改 uri 以欺骗 ftpwebrequest 使用不同的请求(感谢 Jon).

所以我正在寻找第 3 方客户...

我尝试过的一些开源解决方案效果不佳(一直崩溃),但我找到了一个通过我的一些初步测试的开源解决方案(.NET FTP 客户端).

解决方案

有一个博文,它展示了如何伪造它 - 基本上你必须将目录放在 URL 上.

我怀疑使用专用的 FTP 库可能会更好 - 一个不会试图将所有内容都强制转换为 WebRequest 做事方式的库.我个人没有为此使用任何 3rd 方库,而是搜索FTP library .NET".找到很多候选人.


jcolebrand(以防 2006 年博客 linkrot 的可能性)

<块引用>

许多客户询问我们如何将 CWD 命令与我们的 FtpWebRequest 一起使用.

答案是:不能直接使用命令,但是可以修改uri参数达到同样的效果.

假设您使用以下格式:

String uri = "ftp://myFtpUserName:myFtpUserPassword@myFtpUrl";FtpWebRequest 请求 = (FtpWebRequest)WebRequest.Create(uri);Request.Method =列表";

<块引用>

以上示例将带您进入用户目录并列出其中的所有内容.现在假设您要向后移动 2 个目录并列出其中的内容(前提是您的用户有权执行此操作).你关闭之前的 FtpWebRequest 并使用这个 uri 发出一个新的

uri = "ftp://myFtpUserName:myFtpUserPassword@myFtpUrl/%2E%2E/%2E%2E";

<块引用>

这相当于使用您的用户凭据登录,然后使用 cd ../../

注意:如果您尝试直接使用 ".." 而不转义它们,则 uri 类将删除它们,因此 "ftp://myFtpUserName:myFtpUserPassword@myFtpUrl/../.." 等价于 "ftp://myFtpUserName:myFtpUserPassword@myFtpUrl/"

现在假设您要转到另一个用户的目录,该目录比根目录高一级.如果您不指定用户名和密码,则相当于以匿名用户身份登录.然后你发出一个新的 FtpWebRequest 和下面的 uri

ftp://myFtpUrl/%2F/anotherUserDir"

<块引用>

这相当于匿名登录然后做

Cd/cd 另一个用户目录

Can someone tell me how to change directories using FtpWebRequest? This seems like it should be an easy thing to do, but I'm not seeing it.

EDIT

I just want to add...I don't have my heart set on FtpWebRequest. If there's a better (easier) way to do FTP in .NET please let me know.


Apparently there's no way to do it using a live connection, you need to change the uri to trick ftpwebrequest into using a different request (thanks Jon).

So I'm looking for a 3rd party client...

Some of the open source solutions I tried didn't work too well (kept crashing), but I found one open source solution that's passed some of my preliminary tests (.NET FTP Client).

解决方案

There's a blog post from Mariya Atanasova which shows how you can fake it - basically you have to put the directory on the URL.

I suspect you may be better off with a dedicated FTP library though - one that doesn't try to force everything into the WebRequest way of doing things. I haven't personally used any 3rd party libraries for this, but a search for "FTP library .NET" finds lots of candidates.


Edit: jcolebrand (in case of 2006 blog linkrot possibility)

Many customers ask us how they can use the CWD command with our FtpWebRequest.

The answer is: you cannot use the command directly, but you can modify the uri parameter to achieve the same result.

Let's say you're using the following format:

String uri = "ftp://myFtpUserName:myFtpUserPassword@myFtpUrl";
FtpWebRequest Request = (FtpWebRequest)WebRequest.Create(uri);
Request.Method = "LIST";

The above example will bring you to your user's directory and list all the contents there. Now let's say you want to go 2 directories backwards and list the contents there (provided your user has permissions to do that). You close the previous FtpWebRequest and issue a new one with this uri

uri = "ftp://myFtpUserName:myFtpUserPassword@myFtpUrl/%2E%2E/%2E%2E";

This is equivalent to logging in with your user's credentials and then using cd ../../

Note: if you try using the ".." directly without escaping them the uri class will strip them, so "ftp://myFtpUserName:myFtpUserPassword@myFtpUrl/../.." is equivalent to "ftp://myFtpUserName:myFtpUserPassword@myFtpUrl/"

Now let's say you want to go to another user's directory which is one level above the root. If you don't specify a user name and password it's equivalent to logging in as anonymous user. Then you issue a new FtpWebRequest with the following uri

"ftp://myFtpUrl/%2F/anotherUserDir"

This is equivalent to logging in as anonymous and then doing

Cd /
cd anotherUserDirectory

这篇关于如何使用 FtpWebRequest (.NET) 更改目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Custom Error Queue Name when using EasyNetQ for RabbitMQ?(使用 EasyNetQ for RabbitMQ 时自定义错误队列名称?)
Rabbitmq Ack or Nack, leaving messages on the queue(Rabbitmq Ack 或 Nack,将消息留在队列中)
Wait for a single RabbitMQ message with a timeout(等待一条带有超时的 RabbitMQ 消息)
Setup RabbitMQ consumer in ASP.NET Core application(在 ASP.NET Core 应用程序中设置 RabbitMQ 消费者)
How do I set a number of retry attempts in RabbitMQ?(如何在 RabbitMQ 中设置重试次数?)
WebClient set headers(WebClient 设置标头)