The SFTP and the FTP/FTPS are two completely different protocols. You cannot use the FTP to upload to an SFTP server and vice versa. The FTPS is FTP over TLS/SSL session. Most FTP clients/libraries do support the FTPS as well.
Only the FTP(S) is supported natively by the .NET framework (via the FtpWebRequest
class). To use the FTPS, set the FtpWebRequest.EnableSsl
to true
.
There's no native support for the SFTP in the .NET framework. You have to use a 3rd party library for the SFTP.
There are libraries that offer an uniform interface to all these protocols.
For example with the WinSCP .NET assembly, it is (almost) only about setting the SessionOptions.Protocol
to the Protocol.FTP
or the Protocol.SFTP
.
SFTP protocol:
SessionOptions sessionOptions = new SessionOptions {
Protocol = Protocol.Sftp,
HostName = "example.com",
UserName = "user",
Password = "mypassword",
SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx...="
};
Session session = new Session();
session.Open(sessionOptions);
FTP protocol:
SessionOptions sessionOptions = new SessionOptions {
Protocol = Protocol.Ftp,
HostName = "example.com",
UserName = "user",
Password = "mypassword",
};
Session session = new Session();
session.Open(sessionOptions);
FTPS protocol:
SessionOptions sessionOptions = new SessionOptions {
Protocol = Protocol.Ftp,
FtpSecure = FtpSecure.Explicit,
HostName = "example.com",
UserName = "user",
Password = "mypassword",
};
Session session = new Session();
session.Open(sessionOptions);
If you need to make the session configurable, you can make it easy for you by using the SessionOptions.ParseUrl
that allows you to configure major session options using a single connection string (URL) that you set in your configuration file.
SessionOptions sessionOptions = new SessionOptions();
sessionOptions.ParseUrl(connectionString);
Session session = new Session();
session.Open(sessionOptions);
The connection string can be like:
- SFTP:
sftp://user@mypassword;fingerprint=ssh-rsa-xxxxxxxxxxx...=@example.com
- FTP:
ftp://user@mypassword@example.com
- FTPS:
ftpes://user@mypassword@example.com
You can have WinSCP (GUI) generate the URL (connection string) for you.
Note that the WinSCP .NET assembly is not a native .NET library. It's just a thin .NET wrapper around a console application (WinSCP).
There might be native .NET libraries that support all the protocols with an uniform interface. But I'm not aware of any free one.
(I'm the author of WinSCP)