Provides high-level access to FTP server functions. TODO
Provides low-level access to FTP server functions. In most cases using of FtpClient is preferable. It has more understandable interface, if you are not familiar with FTP protocol specifications.
- IPv6 support
Simple example:
var ftp = new FtpConnection();
// connect using host name and port
ftp.Connect("your.hostname.com", 21);
If you need to connect by IP address, use overload of Connect method:
ftp.Connect(IPAddress.Parse("127.0.0.1"), 21);
You can also connect to IPv6 address:
ftp.Connect(IPAddress.Parse("::1"), 21);
If you need to use IPv6 and host name, you can specify to prefer IPv6 address. By default IPv4 address is preferred:
ftp.PreferIPv4 = false;
// if there is associated to this domain IPv6 address, it will be used.
// If there is no IPv6 addresses, IPv4 address will be used.
ftp.Connect("your.hostname.com", 21);
Quit method will send QUIT command to FTP server and close underlying socket.
ftp.Quit();
Simple example:
var ftp = new FtpConnection();
ftp.Connect("your.hostname.com", 21);
ftp.Login("anonymous", "anonymous@gmail.com");
You can also specify account information:
ftp.Login("admin", "qwerty");
ftp.Account("account_data");
Sometimes you need to log out, and keeping connection opened, login by using another credentials:
ftp.Login("admin1", "qwerty1");
// do some actions as admin1
ftp.Reinitialize();
ftp.Login("admin2", "qwerty2");
// do action as admin2
ftp.Quit();
Use EnterPassiveMode or EnterPassiveModeEx method. EnterPassiveMode supports only IPv4, EnterPassiveModeEx supports both IPv4 and IPv6. You should explicitly enter passive or active mode before make any data transfer:
ftp.EnterPassiveMode();
ftp.RetrieveFile("1.txt", stream1);
ftp.EnterPassiveMode();
ftp.RetrieveFile("2.txt", stream2);
Use EnterActiveMode or EnterActiveModeEx method. EnterActiveMode supports only IPv4, EnterActiveModeEx supports both IPv4 and IPv6. You should explicitly enter passive or active mode before make any data transfer:
ftp.EnterActiveMode(IPAddress.Parse("127.0.0.1"), 60000);
ftp.RetrieveFile("1.txt", stream);
ftp.EnterActiveModeEx(IPAddress.Parse("::1"), 60000);
ftp.RetrieveFile("1.txt", stream);
Default mode is Stream. Other modes are not implemented.
ftp.SetTransferMode(FtpTransferMode.Stream);
Example:
ftp.SetRepresentationType(FtpRepresentation.Type.ASCII, FtpRepresentation.Param.NonPrint);
Before each operation you should explicitly enter active or passive mode by using corresponding methods. Simple example:
ftp.EnterPassiveModeEx();
var fileStream = File.OpenWrite("1.txt");
ftp.RetrieveFile("1.txt, fileStream);
fileStream.Close();
ftp.EnterPassiveModeEx();
var fileStream = File.OpenRead("2.txt");
ftp.StoreFile("2.txt, fileStream);
fileStream.Close();
You can resume downloading or uploading from specified position:
// download starts from 100th byte
ftp.RetrieveFile("1.txt, 100, stream);
// upload starts from 100th byte
ftp.StoreFile("2.txt, 100, stream);
You can also append files:
ftp.EnterPassiveModeEx();
ftp.AppendFile("3.txt", stream);
Example:
ftp.EnterPassiveModeEx();
var stream = new MemoryStream();
var cts = new CancellationTokenSource(5000); // download will abort after 5 seconds
var progress = new Progress<long>((p) => Console.WriteLine("Downloaded {0} bytes...", p)); // display progress
var task = ftp.RetrieveFileAsync("test.txt", stream, cts.Token, progress);
task.Wait();
You can also resume download/upload from specified position:
// upload starts from 100th byte
var task = ftp.StoreFileAsync("test.txt", 100, stream, cts.Token, progress);
ftp.RenameFile("old.txt", "new.txt");
ftp.DeleteFile("db.bak");
ftp.MakeDirectory("pub");
ftp.RemoveDirectory("pub");
ftp.ChangeWorkingDirectory("pub");
ftp.ChangeToParentDirectory();
ftp.AuthTls();
You can specify client certificates by using X509CertificateCollection property:
ftp.X509CertificateCollection.Add(new X509Certificate("cert.p12"));
ftp.AuthTls();
You can override certificate validation:
ftp.CertificateValidationCallback = /* your method */;
ftp.AuthTls();
For testing purpose you can accpect all certificates:
ftp.CertificateValidationCallback = delegate { return true; };
ftp.AuthTls();
ftp.AuthTls();
ftp.ProtectionBufferSize(0);
ftp.DataChannelProtectionLevel(FtpDataChannelProtectionLevel.Private);
// any file transfter will use ssl now
This can be used for firewalls, if your ports are closed by default, and firewall scans traffic from FTP connection:
ftp.AuthTls();
ftp.Login("admin", "qwerty"); // send credentials under secure connection
ftp.ClearCommandChannel(); // retun back to unsecure connection
ftp.EnterPassiveMode(); // firewall can parse response from server and allow connection by specified port
ftp.Log = Console.Out;
ftp.Proxy = new Socks4("127.0.0.1", 1001);