FTPClient如何删除目录?
我想删除 FTP 中的一个文件夹。
FTPClient
对象可以删除它吗?
I want to delete a folder in FTP.
Can FTPClient
object delete it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我想删除 FTP 中的一个文件夹。
FTPClient
对象可以删除它吗?
I want to delete a folder in FTP.
Can FTPClient
object delete it?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
要删除空目录,请使用
FtpWebRequest
的RemoveDirectory
“方法”:使用它如下:
虽然它变得更复杂,但如果您需要删除非空目录。
FtpWebRequest
类(或 .NET 框架中的任何其他 FTP 实现)不支持递归操作。您必须自己实现递归:棘手的部分是从子目录中识别文件。无法使用
FtpWebRequest
以可移植的方式做到这一点。不幸的是,FtpWebRequest
不支持MLSD
命令,这是在 FTP 协议中检索具有文件属性的目录列表的唯一可移植方法。另请参阅检查 FTP 服务器上的对象是文件还是目录。您的选择是:
LIST
command =ListDirectoryDetails
方法)并尝试解析特定于服务器的列表。许多 FTP 服务器使用 *nix 样式的列表,您可以通过条目开头的d
来标识目录。但许多服务器使用不同的格式。以下示例使用此方法(假设为 *nix 格式)。使用它的方式与之前的(扁平)实现相同。
尽管 Microsoft 不建议使用
FtpWebRequest
一个新的发展。或者使用支持递归操作的第三方库。
例如,使用 WinSCP .NET 程序集,您可以通过一次调用
Session.RemoveFiles
:在内部,WinSCP 使用
MLSD
命令(如果服务器支持)。如果没有,它会使用LIST
命令并支持数十种不同的列表格式。(我是 WinSCP 的作者)
To delete an empty directory, use the
RemoveDirectory
"method" of theFtpWebRequest
:use it like:
Though it gets a way more complicated, if you need to delete a non-empty directory. There's no support for recursive operations in
FtpWebRequest
class (or any other FTP implementation in the .NET framework). You have to implement the recursion yourself:Tricky part is to identify files from subdirectories. There's no way to do that in a portable way with the
FtpWebRequest
. TheFtpWebRequest
unfortunately does not support theMLSD
command, which is the only portable way to retrieve directory listing with file attributes in FTP protocol. See also Checking if object on FTP server is file or directory.Your options are:
LIST
command =ListDirectoryDetails
method) and try to parse a server-specific listing. Many FTP servers use *nix-style listing, where you identify a directory by thed
at the very beginning of the entry. But many servers use a different format. The following example uses this approach (assuming the *nix format).Use it the same way as the previous (flat) implementation.
Though Microsoft does not recommend
FtpWebRequest
for a new development.Or use a 3rd party library that supports recursive operations.
For example with WinSCP .NET assembly you can delete whole directory with a single call to
Session.RemoveFiles
:Internally, WinSCP uses the
MLSD
command, if supported by the server. If not, it uses theLIST
command and supports dozens of different listing formats.(I'm the author of WinSCP)
我发现工作的唯一方法是依赖“WebRequestMethods.Ftp.DeleteFile”,它会在文件夹或带有文件的文件夹的情况下给出例外,因此我创建了一个新的内部方法来递归删除目录
这是代码
和目录删除
List Direcotry Children
The only way I found working is to rely on "WebRequestMethods.Ftp.DeleteFile" and It will give an exception incase of folders or folders with files so I created a new interenal method to deletedirectory recursively
here is the code
and directory deletion
List Direcotry children
FtpWebRequest 提供删除操作。
这是实现该目的的一段代码:
它应该适用于文件和目录。事实上,请检查您是否拥有正确的权限。
此外,当文件夹不为空时,您无法删除它们。您必须递归遍历它们才能删除之前的内容。
由于权限问题引发的异常并不总是很清楚......
FtpWebRequest provides the Delete action.
Here is a piece of code to achieve that :
It should work on files and directories. Indeed, please check that you have the right permissions.
Also, you could not delete folders while they are not empty. You must traverse them recursively to delete content before.
The exceptions thrown due to right permissions problems are not always very clear...
重要点
如上所述..
当文件夹不为空时您无法删除它们。您必须递归遍历它们才能删除之前的内容。
Important Point
As mentioned above..
you could not delete folders while they are not empty. You must traverse them recursively to delete content before.
这就是您可以使用的代码。
以下是您如何使用它,例如,单击按钮。
请记住,您的文件夹应该是空的。
That's the code you can use.
And here's how you can use it, for example, on a button click.
And just remember that your folder should be EMPTY.