.Net 6:创建 FtpWebRequest 对象的正确方法
在c#使用.NET6中,我们可以使用 ftpwebrequest
-Object。这效果很好。但是我们应该如何创建这个对象?
目前,我们使用
和。
但是现在这个功能已经过时了。因此,当创建
- 功能已过时,但是 ftpwebrequest
type不是,那么创建 ftpwebrequest
-Object的正确和推荐方法是什么不使用过时的功能?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
微软明确表示要与第三方合作,并且不要使用他们的任何内置库:
https://github.com/dotnet/platform-compat/blob/master/docs/DE0003.md
他们说“从以下列表中选择第三方库”并且添加指向 StackOverflow 的链接,指向以下线程:
免费 FTP 库
Microsoft clearly says to work with third-party and do not use any of their built-in libraries:
https://github.com/dotnet/platform-compat/blob/master/docs/DE0003.md
They says 'pick up a third-party library from the following list' and adding a link that pointing to StackOverflow, to the following thread:
Free FTP Library
根据 Microsoft Learn 文档,FtpWebRequest 类实现可用于与 FTP 服务器交互的文件传输协议 (FTP) 客户端。要创建此类的实例,您可以使用 WebRequest.Create 方法并将结果转换为 FtpWebRequest。但是,该方法在.NET 6及更高版本中被标记为过时,因为它主要用于创建HttpWebRequest实例,不再推荐使用。
该文档建议使用支持 FTP 的第三方库,例如 FluentFTP,而不是使用 FtpWebRequest 类。该库提供了一种更现代、更高效的 FTP 服务器工作方式,并且支持异步/等待、SSL/TLS、代理、散列和许多其他功能。
或者,您可以使用 WebClient 类执行基本 FTP 操作,例如上传和下载文件,而不将结果强制转换为 FtpWebRequest。 WebClient 类还支持异步/等待和取消标记,并且它没有标记为已过时。您可以在 Microsoft Learn 文档和 CodeProject 文章。
According to the Microsoft Learn documentation, the FtpWebRequest class implements a File Transfer Protocol (FTP) client that you can use to interact with an FTP server. To create an instance of this class, you can use the WebRequest.Create a method and cast the result to FtpWebRequest. However, this method is marked as obsolete in .NET 6 and later versions, because it is mainly used for creating HttpWebRequest instances, which are no longer recommended.
The documentation suggests using a third-party library that supports FTP, such as FluentFTP, instead of using the FtpWebRequest class. This library provides a more modern and efficient way of working with FTP servers, and it supports async/await, SSL/TLS, proxies, hashing, and many other features.
Alternatively, you can use the WebClient class to perform basic FTP operations, such as uploading and downloading files, without casting the result to FtpWebRequest. The WebClient class also supports async/await and cancellation tokens, and it is not marked as obsolete. You can find some examples of using the WebClient class for FTP in the Microsoft Learn documentation and the CodeProject article.