22

Can I download file / list files via FTP protocol using netcoreapp1.0?

I know, I can use FtpWebRequest or FluentFTP if I target full .net45 framework.

My solution, however, is all based on .NET Standard 1.6 and I don't want to support full framework just to have FTP.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Sergei Rudakov
  • 558
  • 1
  • 4
  • 11

5 Answers5

13

FluentFTP now supports .NET core / .NET standard 1.6. If you run into problems please add an issue in the issue tracker and we'll work on it.

Edit 1: Examples of using: https://github.com/robinrodricks/FluentFTP/tree/master/FluentFTP.CSharpExamples

sebastian.roibu
  • 2,579
  • 7
  • 37
  • 59
Robin Rodricks
  • 110,798
  • 141
  • 398
  • 607
13

Edit for .net 6

FtpWebRequest is deprecated in .net 6.0.

As such, consider using FluentFTP (previously known as System.Net.FtpClient).

It is released under The MIT License and available on NuGet.

For prior .net versions:

FtpWebRequest is now supported in .NET Core 2.0. See GitHub repo

Example usage:

public static byte[] MakeRequest(
    string method, 
    string uri, 
    string username, 
    string password, 
    byte[] requestBody = null)
{
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
    request.Credentials = new NetworkCredential(username, password);
    request.Method = method;
    //Other request settings (e.g. UsePassive, EnableSsl, Timeout set here)
    
    if (requestBody != null)
    {
        using (MemoryStream requestMemStream = new MemoryStream(requestBody))
        using (Stream requestStream = request.GetRequestStream())
        {
            requestMemStream.CopyTo(requestStream);
        }
    }

    using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
    using (MemoryStream responseBody = new MemoryStream())
    {
        response.GetResponseStream().CopyTo(responseBody);
        return responseBody.ToArray();
    }
}

Where the value for the method parameter is set as a member of System.Net.WebRequestMethods.Ftp.

See also FTP Examples

SpruceMoose
  • 9,737
  • 4
  • 39
  • 53
  • `WebRequest` is [deprecated](https://learn.microsoft.com/en-us/dotnet/core/compatibility/networking/6.0/webrequest-deprecated) in .Net 6. They write about it: `For FTP, since HttpClient doesn't support it, we recommend using a third-party library.` So, the way to go is now to use a third party library like FluentFTP. – Frédéric Dec 31 '21 at 13:23
12

FtpWebRequest is now included to .NET Standard 2.0

FluentFTP library is also compatible with latest .net standard 2.0

Sergei Rudakov
  • 558
  • 1
  • 4
  • 11
  • if you need sftp support, FluentFTP doesn't support, but https://github.com/sshnet/SSH.NET does and supports .NET Standard 1.3. – gabe May 01 '18 at 21:34
  • 6
    Note: DE0003: WebRequest shouldn't be used - https://github.com/dotnet/platform-compat/blob/master/docs/DE0003.md – StarQuake Jan 21 '19 at 12:38
  • is FtpWebRequest use in .netcore application? – jithu Oct 06 '20 at 04:04
  • 1
    `WebRequest` is [deprecated](https://learn.microsoft.com/en-us/dotnet/core/compatibility/networking/6.0/webrequest-deprecated) in .Net 6. They write about it: `For FTP, since HttpClient doesn't support it, we recommend using a third-party library.` So, the way to go is now to use a third party library like FluentFTP. – Frédéric Dec 31 '21 at 13:22
6

There are no FTP capabilities out-of-the-box for netcoreapp1.0 or netstandard1.6. FtpWebRequest will return in netstandard2.0.

Bakudan
  • 19,134
  • 9
  • 53
  • 73
vcsjones
  • 138,677
  • 31
  • 291
  • 286
  • Make sense. Can't wait for 2.0 so I had to create small .net46 app that downloads files from FTP and stores them in local folder. Main .netcore app uses FileSystemWatcher to monitor the folder and process these files when they appear. – Sergei Rudakov Nov 15 '16 at 18:47
5

You can try using FtpWebRequest method.

Sample:

public static byte[] DownloadFile(string url, string filePath, string user, string password)
        {
            var ftpServerUrl = string.Concat(url, filePath);
            var request = (FtpWebRequest) WebRequest.Create(ftpServerUrl);
            request.Method = WebRequestMethods.Ftp.DownloadFile;

            request.Credentials = new NetworkCredential(user,password);
            using (var response = (FtpWebResponse) request.GetResponse())
            using (var responseStream = response.GetResponseStream())
            using (var memoryStream = new MemoryStream())
            {
                responseStream?.CopyTo(memoryStream);
                return memoryStream.ToArray();
            }
        }

Keep in mind, that the the ftpServerUrl has to be a valid uri path containing the file path. e.g. ftpServerUrl = "ftp://ftp.server/targetfile.txt"

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
George Kargakis
  • 4,940
  • 3
  • 16
  • 12