gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 05:22:21 28 4
gpt4 key购买 nike

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

编辑

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

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

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

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

最佳答案

有一个 blog post来自 Mariya Atanasova,它展示了如何伪造它 - 基本上你必须将目录放在 URL 上。
我怀疑您可能会更好地使用专用的 FTP 库 - 一个不会试图将所有内容强制采用 WebRequest 做事方式的库。我个人没有为此使用任何 3rd 方库,但是搜索“FTP 库 .NET”会发现很多候选库。

编辑: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:

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

关于.net - 如何使用 FtpWebRequest (.NET) 更改目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/330155/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com