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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/330155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:43:23  来源:igfitidea点击:

How do you change directories using FtpWebRequest (.NET)?

.netftpftpwebrequest

提问by Giovanni Galbo

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.

有人可以告诉我如何使用 FtpWebRequest 更改目录吗?这似乎应该是一件容易的事情,但我没有看到。

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.

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



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).

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

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

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

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).

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

回答by Jon Skeet

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

一篇来自 Mariya Atanasova的博客文章展示了如何伪造它 - 基本上你必须将目录放在 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.

我怀疑您可能会更好地使用专用的 FTP 库 - 一个不会试图将所有内容强制转换为 WebRequest 做事方式的库。我个人没有为此使用任何 3rd 方库,但是搜索“FTP 库 .NET”会发现很多候选库。



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

编辑:jcolebrand(在 2006 年博客链接可能的情况下)

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:

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

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

假设您使用以下格式:

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

上面的示例会将您带到您的用户目录并列出那里的所有内容。现在假设您要向后移动 2 个目录并列出其中的内容(前提是您的用户有权这样做)。你关闭之前的 FtpWebRequest 并用这个 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 FtpWebRequestwith the following uri

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

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

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

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

This is equivalent to logging in as anonymous and then doing

这相当于以匿名身份登录然后执行

Cd /
cd anotherUserDirectory

回答by Giochi Blu

You have to close the current connection:

您必须关闭当前连接:

request.Close();

And open a new one with an other uri:

并用另一个 uri 打开一个新的:

uri = "ftp://example.com/%2F/directory" //Go to a forward directory (cd directory)
uri = "ftp://example.com/%2E%2E" //Go to the previously directory (cd ../)

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);

回答by Chintan Udeshi

Instead of using ListDirectory method of FTPWebRequest you can use ListDirectoryDetails method of FtpWebRequest .

您可以使用 FtpWebRequest 的 ListDirectoryDe​​tails 方法,而不是使用 FTPWebRequest 的 ListDirectory 方法。

From That you can use regular expression to get value you want. Thats it,It work fine for me in my case

从那个你可以使用正则表达式来获得你想要的价值。就是这样,就我而言,它对我来说很好用