Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FTP client in .NET Core

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.

like image 337
Sergei Rudakov Avatar asked Nov 15 '16 00:11

Sergei Rudakov


People also ask

Where is the FTP client?

An FTP client works on a client/server architecture, where the host computer is the client and the remote FTP server is the central server.

Do I need FTP client?

In order to use FTP, you will need an FTP client which is a desktop app that connects your computer to your WordPress hosting account. It provides an easy to use graphics user interface, so that you can perform all FTP functions such as copy, upload, delete, rename, and edit files / folders on your WordPress site.


2 Answers

FtpWebRequest is now included to .NET Standard 2.0

FluentFTP library is also compatible with latest .net standard 2.0

like image 61
Sergei Rudakov Avatar answered Oct 07 '22 13:10

Sergei Rudakov


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

like image 42
SpruceMoose Avatar answered Oct 07 '22 15:10

SpruceMoose