Search code examples
c#.netftpftpwebrequest

FTP multiple files in C# without reestablishing connection


The FTP protocol is designed to support a control channel, and use that control channel to tell the server to open TCP connections and transfer files.

The server sending or receiving files does NOT have to be the same as the server that the FTP control channel is connected to. It can be a "triangle" type connection.

It also allows the client to log in once on the control channel, and repeatedly tell the server to transfer files, without re-logging into the control channel.

Apparently, this concept has completely escaped MS when they created the C# FtpWebRequest class.

I need to do exactly what the FTP protocol was designed to do:

  1. Connect to a server

  2. Pass in credentials

  3. Create directories (and happily ignore an 'already exists' error)

  4. Repeatedly transfer files up to the server

  5. Log out of the control channel

I sure am not seeing that ability in the FtpWebRequest class. Or anything that would appear to allow that kind of a flow in C# code.

I have looked:

But none of this seems to allow control that way it was meant to be.

I can specify the KeepAlive property, but the loop has to repeatedly call the WebRequest.Create(targetName); function, which would create a NEW connection, and get a NEW response. Then they fall out of scope or are orphaned, so by definition, they are destroyed. Therefore the connection MUST be closed, and then will have to be reopened. For a data connection, that's okay, but where is the ability to manipulate the CONTROL port?

The class doesn't allow the user to differentiate from a CONTROL port and a DATA port, as the FTP specification defines.

Is there a ways to use a C# class to do FTP the way it was meant to be? Because in Microsoft's narrow mindset, the whole world looks like an HTTP Get/Response protocol.

Any advice is appreciated.

-Scotty


Solution

  • edtFTPnet looks promising, as did FluentFTP

    However, in the end, using a DLL assembly or library for this type of project, whether with the LGPL or MIT or anything else, opens up the possibility of a malicious attack by someone replacing the DLL with one which was injected with some form of Malware. Because the source is freely available.

    In the end, it is better to write your own, with the code embedded directly in the executable. The extra effort is worth the security.

    So I am marking this as answered. The comments were appreciated.