4

In C# using .Net6 we can interact with an FTP-server using an FtpWebRequest-object. This works well. But how should we create this object? Currently we use WebRequest.Create() and Microsoft says we should do exactly this.

But this function is obsolete now. So when the Create-function is obsolete but the FtpWebRequest-type is not, what is the correct and recommended way of creating a FtpWebRequest-object without using obsolete functions?

anion
  • 1,516
  • 1
  • 21
  • 35
  • Arguably the method is obsolete only for the specific purpose of instantiating an `HttpWebRequest`. This also happens to be the most common use, so the `ObsoleteAttribute` makes sense. This attribute can't be made sophisticated enough to detect the non-obsolete use cases. I'd simply suppress the warning in this case. If you feel uncomfortable doing this every time, a simple helper method could be written (`CreateFtpWebRequest(Uri uri) => uri.Scheme != "ftp" ? throw new ArgumentException(...) : (FtpWebRequest) WebRequest.Create(uri)`, add `#pragma warning disable` to taste). – Jeroen Mostert Apr 06 '22 at 13:54
  • 4
    As Microsoft says you should use third-party library that ``FluentFtp`` is the best I think, https://learn.microsoft.com/en-us/dotnet/core/compatibility/networking/6.0/webrequest-deprecated – sa-es-ir Apr 06 '22 at 13:57
  • Does this answer your question? [FTP client in .NET Core](https://stackoverflow.com/questions/40600312/ftp-client-in-net-core) – Jay Zuo Nov 03 '22 at 02:40

0 Answers0