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...
所以我正在寻找第三方客户......
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)。
3 个解决方案
#1
12
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.
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做事的方式。我没有亲自使用任何第三方库,但搜索“FTP库.NET”找到了很多候选人。
Edit: jcolebrand (in case of 2006 blog linkrot possibility)
Many customers ask us how they can use the CWD command with our FtpWebRequest.
许多客户问我们如何在我们的FtpWebRequest中使用CWD命令。
The answer is: you cannot use the command directly, but you can modify the uri parameter to achieve the same result.
答案是:您不能直接使用该命令,但您可以修改uri参数以获得相同的结果。
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
上面的示例将带您进入用户的目录并列出其中的所有内容。现在假设您要向后移动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 ../../
这相当于使用您的用户凭据登录,然后使用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/"
注意:如果您尝试直接使用“..”而不转义它们,则uri类将删除它们,因此“ftp:// myFtpUserName:myFtpUserPassword @ myFtpUrl /../ ..”相当于“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现在假设您要转到另一个用户的目录,该目录位于根目录上方一级。如果您未指定用户名和密码,则相当于以匿名用户身份登录。然后使用以下uri发出新的FtpWebRequest
"ftp://myFtpUrl/%2F/anotherUserDir"
This is equivalent to logging in as anonymous and then doing
这相当于以匿名身份登录然后执行
Cd /
cd anotherUserDirectory
#2
3
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);
#3
2
Instead of using ListDirectory method of FTPWebRequest you can use ListDirectoryDetails method of FtpWebRequest .
而不是使用FTPWebRequest的ListDirectory方法,您可以使用FtpWebRequest的ListDirectoryDetails方法。
From That you can use regular expression to get value you want. Thats it,It work fine for me in my case
从那你可以使用正则表达式来获得你想要的价值。多数民众赞成在我的情况下,它对我来说很好
#1
12
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.
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做事的方式。我没有亲自使用任何第三方库,但搜索“FTP库.NET”找到了很多候选人。
Edit: jcolebrand (in case of 2006 blog linkrot possibility)
Many customers ask us how they can use the CWD command with our FtpWebRequest.
许多客户问我们如何在我们的FtpWebRequest中使用CWD命令。
The answer is: you cannot use the command directly, but you can modify the uri parameter to achieve the same result.
答案是:您不能直接使用该命令,但您可以修改uri参数以获得相同的结果。
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
上面的示例将带您进入用户的目录并列出其中的所有内容。现在假设您要向后移动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 ../../
这相当于使用您的用户凭据登录,然后使用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/"
注意:如果您尝试直接使用“..”而不转义它们,则uri类将删除它们,因此“ftp:// myFtpUserName:myFtpUserPassword @ myFtpUrl /../ ..”相当于“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现在假设您要转到另一个用户的目录,该目录位于根目录上方一级。如果您未指定用户名和密码,则相当于以匿名用户身份登录。然后使用以下uri发出新的FtpWebRequest
"ftp://myFtpUrl/%2F/anotherUserDir"
This is equivalent to logging in as anonymous and then doing
这相当于以匿名身份登录然后执行
Cd /
cd anotherUserDirectory
#2
3
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);
#3
2
Instead of using ListDirectory method of FTPWebRequest you can use ListDirectoryDetails method of FtpWebRequest .
而不是使用FTPWebRequest的ListDirectory方法,您可以使用FtpWebRequest的ListDirectoryDetails方法。
From That you can use regular expression to get value you want. Thats it,It work fine for me in my case
从那你可以使用正则表达式来获得你想要的价值。多数民众赞成在我的情况下,它对我来说很好