如何使用 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(incaseof2006bloglinkrotpossibility)

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) 更改目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)