1

I'm migrating my FTPSender project to .net 6. I was using FTPClient as below

public bool UploadFile(string sourceFile,string destinationUrl)
        {
             try
            {
                if (ftpType == FtpTypes.SSH)
                {

                    Sftp sftp = new Sftp(hostAdress, userName, password);
                    sftp.Connect(22);
                    sftp.Put(sourceFile, destinationUrl);
                    sftp.Close();

                    if (destinationUrl.Contains("#"))
                    {
                        var sshExec = new SshExec(hostAdress, userName, password);
                        sshExec.Connect();
                        var err = string.Empty;
                        var outx = string.Empty;
                        sshExec.RunCommand("mv " + destinationUrl + " " + destinationUrl.Replace("#", ""), ref outx, ref err);
                        sshExec.Close();
                    }

                }
                else
                {
                    FileInfo file = new FileInfo(sourceFile);
                    FtpWebRequest reqFTP;
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(hostAdress + destinationUrl));
                    reqFTP.Credentials = new NetworkCredential(userName, password);
                    reqFTP.KeepAlive = false;
                    reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
                    reqFTP.UseBinary = true;
                    reqFTP.ContentLength = file.Length;
                    int buffLength = 2048;
                    byte[] buff = new byte[buffLength];
                    int contentLen;
                    FileStream fs = file.OpenRead();
                    Stream strm = reqFTP.GetRequestStream();
                    contentLen = fs.Read(buff, 0, buffLength);
                    while (contentLen != 0)
                    {
                        strm.Write(buff, 0, contentLen);
                        contentLen = fs.Read(buff, 0, buffLength);
                    }
                    strm.Close();
                    fs.Close();
                }
            }
            catch(Exception ex)
            {
                return false;

            }

            return true;
            
        }

I copied the exact copy on .net 6. But it gives error SYSLIB0014.

WebRequest, HttpWebRequest, ServicePoint, WebClient are obsolete.Use HttpClient instead.

When I enter the HttpClient page another warning is

For FTP, since HttpClient doesn't support it, we recommend using a third-party library.

Is WebRequest still available or What should i use? How to implement it?

  • Mmm.. This is quite close to a "recommend me an FTP library" which is off-topic.. Ther are other questions that have asked this before, and have been answered.. – Caius Jard Jan 18 '22 at 09:20
  • Does it still work if you just suppress the warning with `#pragma warning disable SYSLIB0014`? While it is obsolete, it should still work. If you suppress the warning for now, you can move on with getting your app working as-is, then you could come back to review a newer implementation to replace it later. https://learn.microsoft.com/en-us/dotnet/fundamentals/syslib-diagnostics/syslib0014 – Martin Costello Jan 18 '22 at 09:28
  • @CaiusJard Can you show me a few threads related to this topic? I didn't find – midoriyakuzan Jan 18 '22 at 11:12
  • 1
    Honestly, [this](https://stackoverflow.com/questions/1371964/free-ftp-library) was the first hit on duckduckgo for "c# ftp library" - I've used edFTP before and found it adequate. The WinSCP author Martin Prikryl posts here and is good for support on questions avbout WinSCP's NET wrapper – Caius Jard Jan 18 '22 at 16:39

0 Answers0