問題描述
誰能告訴我如何使用 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) 更改目錄?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!