public void Set(string id, FtpsClient client) { if (_ftpClients.ContainsKey(id)) { _ftpClients[id] = client; } else { if (_ftpClients.Count == 0) { // dispose of the ftp on shutdown Plug.OnShutDown += DisconnectFtp; } _ftpClients.Add(id, client); } }
/// <summary> /// Connects to a FTP server trying every methods /// </summary> private void ConnectFtp(FtpsClient ftp, string userName, string passWord, string server, int port) { NetworkCredential credential = null; if (!string.IsNullOrEmpty(userName)) { credential = new NetworkCredential(userName, passWord); } var modes = new List <EsslSupportMode>(); typeof(EsslSupportMode).ForEach <EsslSupportMode>((s, l) => { modes.Add((EsslSupportMode)l); }); ftp.DataConnectionMode = EDataConnectionMode.Passive; while (!ftp.Connected && ftp.DataConnectionMode == EDataConnectionMode.Passive) { foreach (var mode in modes.OrderByDescending(mode => mode)) { try { var curPort = port > -1 ? port : ((mode & EsslSupportMode.Implicit) == EsslSupportMode.Implicit ? 990 : 21); ftp.Connect(server, curPort, credential, mode, 1800); ftp.Connected = true; break; } catch (Exception) { //ignored } } ftp.DataConnectionMode = EDataConnectionMode.Active; } // failed? if (!ftp.Connected) { throw new Exception("Failed to connect to a FTP server with : " + string.Format(@"Username : {0}, Password : {1}, Host : {2}, Port : {3}", userName ?? "none", passWord ?? "none", server, port == -1 ? 21 : port)); } }