//public void DownloadFiles(string remoteFiles, string localDirectory, bool deleteRemoteFile = false) //{ // var directoryName = Path.GetDirectoryName(remoteFiles); // var remoteFileNames = GetDirectoryList(directoryName); // foreach (var remoteFileName in remoteFileNames) // { // var localFileName = Path.Combine(localDirectory, Path.GetFileName(remoteFileName)); // DownloadFile(remoteFileName, localFileName, deleteRemoteFile); // } //} public void UploadFile(string localFile, string remoteFile, bool deleteLocalFile = false) { var ftp = new FTPSClient(); try { ftp.Connect(this.host, new NetworkCredential(this.user, this.password), ESSLSupportMode.ClearText); ftp.SetTransferMode(this.UseBinary ? ETransferMode.Binary : ETransferMode.ASCII); //ftp.SetCurrentDirectory(remoteFolder); ftp.PutFile(localFile, remoteFile); if (deleteLocalFile == true) { ftp.DeleteFile(localFile); } } catch (Exception ex) { throw; } finally { if (ftp != null) { ftp.Close(); ftp.Dispose(); } } }
public void ConnectTest() { using (FTPSClient client = new FTPSClient()) { // Connect to the server, with mandatory SSL/TLS // encryption during authentication and // optional encryption on the data channel // (directory lists, file transfers) try { // client.Connect("us1.DOMAIN.com", new NetworkCredential(read-write, password), ESSLSupportMode.CredentialsRequired | ESSLSupportMode.DataChannelRequested); // client.Connect("us1.DOMAIN.com", new NetworkCredential(read-write, password), ESSLSupportMode.CredentialsRequired | ESSLSupportMode.DataChannelRequested); // client.Connect("us1.DOMAIN.com", new NetworkCredential(read-write, password), ESSLSupportMode.ControlAndDataChannelsRequired); // client.Connect("ftp.DOMAIN.com", new NetworkCredential(read-write, password), ESSLSupportMode.ClearText); client.Connect("ftps.DOMAIN.com", new NetworkCredential("username", password), ESSLSupportMode.ControlAndDataChannelsRequested); // client.MakeDir(System.Guid.NewGuid().ToString()); var sys = client.GetSystem(); var current = client.GetCurrentDirectory(); // client.SetCurrentDirectory("/Directory"); // client.GetFile("/Directory/File.ext", "c:\\temp\\File.ext"); } catch (Exception ex) { // handle exception } finally { client.Close(); } } }
public AutoUploadLog(int sessionid) { string val = PackString(FingerPrint.Value(), 9); string uniqueid = "log-" + sessionid.ToString() + "-" + val; FTPSClient fli = new FTPSClient(); fli.Connect("hostname", new System.Net.NetworkCredential("username", "password"), ESSLSupportMode.DataChannelRequested); fli.PutFile(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\BExplorer\\log.txt", uniqueid + ".txt"); fli.Close(); }
/// <summary> /// Disconnect /// </summary> /// <returns></returns> public bool Disconnect() { try { _client.Close(); return(true); } catch (Exception) { return(false); } }
public void Disconnect() { try { if (client != null) { client.Close(); } } catch (Exception ex) { throw ex; } }
public IEnumerable <FtpFileInfo> GetDirectoryList(string remoteDirectory) { var remoteDirectoryName = remoteDirectory; if (!this.IsDirectory(remoteDirectory)) { remoteDirectoryName = this.GetDirectoryName(remoteDirectory); } if (string.IsNullOrEmpty(remoteDirectory)) { remoteDirectory = "."; } var ftp = new FTPSClient(); try { ftp.Connect(this.host, new NetworkCredential(this.user, this.password), ESSLSupportMode.ClearText); ftp.SetTransferMode(this.UseBinary ? ETransferMode.Binary : ETransferMode.ASCII); var ftpFiles = ftp.GetDirectoryList(remoteDirectory); var files = new List <FtpFileInfo>(); foreach (var ftpFile in ftpFiles) { var ftpFileInfo = new FtpFileInfo(); ftpFileInfo.Name = ftpFile.Name; ftpFileInfo.CreationTime = ftpFile.CreationTime; ftpFileInfo.Flags = ftpFile.Flags; ftpFileInfo.IsDirectory = ftpFile.IsDirectory; ftpFileInfo.FullName = Path.Combine(remoteDirectoryName, ftpFileInfo.Name); files.Add(ftpFileInfo); } return(files); } catch (Exception ex) { throw; } finally { if (ftp != null) { ftp.Close(); ftp.Dispose(); } } }
public IEnumerable <string> DownloadFiles(string remoteFiles, string localDirectory, bool deleteRemoteFile = false) { var downloadedFiles = new List <string>(); var ftp = new FTPSClient(); try { ftp.Connect(this.host, new NetworkCredential(this.user, this.password), ESSLSupportMode.ClearText); ftp.SetTransferMode(this.UseBinary ? ETransferMode.Binary : ETransferMode.ASCII); //ftp.SetCurrentDirectory(remoteFolder); var directoryName = this.GetDirectoryName(remoteFiles); var remoteFileInfos = this.GetDirectoryList(directoryName); var remoteFilePattern = Path.GetFileName(remoteFiles); foreach (var remoteFileInfo in remoteFileInfos) { if (string.IsNullOrEmpty(remoteFilePattern) || FileUtil.MatchesWildcard(remoteFileInfo.Name, remoteFilePattern)) { var localFileName = Path.Combine(localDirectory, Path.GetFileName(remoteFileInfo.Name)); ftp.GetFile(remoteFileInfo.FullName, localFileName); downloadedFiles.Add(remoteFileInfo.FullName); if (deleteRemoteFile == true) { ftp.DeleteFile(remoteFileInfo.FullName); } } } } catch (Exception ex) { throw; } finally { if (ftp != null) { ftp.Close(); ftp.Dispose(); } } return(downloadedFiles); }
public void DownloadFile(string remoteFile, string localFile, bool deleteRemoteFile = false) { var ftp = new FTPSClient(); var localDownloadFile = localFile; try { //ftp.Connect(hostname: host, // port: this.port, // credential: new NetworkCredential(this.user, this.password), // sslSupportMode: ESSLSupportMode.All, // userValidateServerCertificate: null, // x509ClientCert: null, // dataConnectionMode: EDataConnectionMode.Passive); ftp.Connect(this.host, new NetworkCredential(this.user, this.password), ESSLSupportMode.ClearText); ftp.SetTransferMode(this.UseBinary ? ETransferMode.Binary : ETransferMode.ASCII); //ftp.SetCurrentDirectory(remoteFolder); if (Directory.Exists(localFile) && !Path.HasExtension(localFile)) { localDownloadFile = Path.Combine(localFile, Path.GetFileName(remoteFile)); } ftp.GetFile(remoteFile, localDownloadFile); if (deleteRemoteFile == true) { ftp.DeleteFile(remoteFile); } } catch (Exception ex) { throw; } finally { if (ftp != null) { ftp.Close(); ftp.Dispose(); } } }
public void CreateDirectory(string newDirectory) { var ftp = new FTPSClient(); try { ftp.Connect(this.host, new NetworkCredential(this.user, this.password), ESSLSupportMode.ClearText); ftp.SetTransferMode(this.UseBinary ? ETransferMode.Binary : ETransferMode.ASCII); ftp.MakeDir(newDirectory); } catch (Exception ex) { throw; } finally { if (ftp != null) { ftp.Close(); ftp.Dispose(); } } }
public override void CloseConnection() { _client.Close(); }
public void Disconnect() { Debug.WriteLine("Disconnecting from FTP " + _host + ":" + _port); client.Close(); Debug.WriteLine("Disconnected from FTP " + _host + ":" + _port); }
public static bool GetCurrentRecords(string Server, string SignalId, string User, string Password, string LocalDir, string RemoteDir, bool DeleteFilesAfterFTP, int SNMPRetry, int SNMPTimeout, int SNMPPort, bool ImportAfterFTP, bool activemode, int waitbetweenrecords, BulkCopyOptions Options, int FTPTimeout) //, CancellationToken Token) { var recordsComplete = false; var errorRepository = ApplicationEventRepositoryFactory.Create(); //Initialize the FTP object var RetryFiles = new List <string>(); var FTP = new FTPSClient(); var Cred = new NetworkCredential(User, Password); var SSLMode = ESSLSupportMode.ClearText; var DM = EDataConnectionMode.Passive; if (activemode) { DM = EDataConnectionMode.Active; } var connected = false; var FilePattern = ".dat"; try { try { var tokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(30)); var token2 = tokenSource.Token; Task task = Task.Factory.StartNew( () => FTP.Connect(Server, 21, Cred, SSLMode, null, null, 0, 0, 0, FTPTimeout, true, DM) , token2); task.Wait(token2); if (token2.IsCancellationRequested) { token2.ThrowIfCancellationRequested(); } } catch (AggregateException) { errorRepository.QuickAdd("FTPFromAllcontrollers", "Signal", "ConnectToController", ApplicationEvent.SeverityLevels.Medium, "The connection task timed out for signal " + Server); } { Console.WriteLine("Connection Failure"); } connected = true; } //If there is an error, Print the error and go on to the next file. catch (FTPException ex) { errorRepository.QuickAdd("FTPFromAllcontrollers", "Signal", "ConnectToController", ApplicationEvent.SeverityLevels.Medium, Server + " " + ex.Message); Console.WriteLine(ex.Message); } catch (AggregateException) { Console.WriteLine("Connection Failure"); } catch (SocketException ex) { errorRepository.QuickAdd("FTPFromAllcontrollers", "Signal", "ConnectToController", ApplicationEvent.SeverityLevels.Medium, Server + " " + ex.Message); Console.WriteLine(ex.Message); } catch (IOException ex) { errorRepository.QuickAdd("FTPFromAllcontrollers", "Signal", "ConnectToController", ApplicationEvent.SeverityLevels.Medium, Server + " " + ex.Message); Console.WriteLine(ex.Message); } catch (Exception ex) { errorRepository.QuickAdd("FTPFromAllcontrollers", "Signal", "ConnectToController", ApplicationEvent.SeverityLevels.Medium, Server + " " + ex.Message); Console.WriteLine(ex.Message); } if (connected) { try { //if (Token.IsCancellationRequested) //{ // Token.ThrowIfCancellationRequested(); //} FTP.SetCurrentDirectory(".."); FTP.SetCurrentDirectory(RemoteDir); } catch (AggregateException) { Console.WriteLine("Connection Failure for " + Server); } catch (Exception ex) { errorRepository.QuickAdd("FTPFromAllcontrollers", "Signal", "GetCurrentRecords", ApplicationEvent.SeverityLevels.Medium, Server + " " + ex.Message); Console.WriteLine(ex.Message); } try { IList <DirectoryListItem> RemoteFiles = null; var tokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(30)); var token = tokenSource.Token; Task task = Task.Factory.StartNew(() => RemoteFiles = FTP.GetDirectoryList(RemoteDir) , token); task.Wait(token); if (token.IsCancellationRequested) { token.ThrowIfCancellationRequested(); } var RetrievedFiles = new List <string>(); //errorRepository.QuickAdd("FTPFromAllcontrollers","Signal", "GetFTPFileList", Models.ApplicationEvent.SeverityLevels.Information, "Retrevied File list from " + Server); if (RemoteFiles != null) { foreach (var FTPFile in RemoteFiles) { if (!FTPFile.IsDirectory && FTPFile.Name.Contains(FilePattern)) { try { //if (Token.IsCancellationRequested) //{ // Token.ThrowIfCancellationRequested(); //} //If there are no errors, get the file, and add the filename to the retrieved files array for deletion later var token2 = tokenSource.Token; var task2 = Task.Factory.StartNew( () => TransferFiles(FTP, FTPFile.Name, LocalDir, RemoteDir, Server) , token2); task2.Wait(token2); if (token2.IsCancellationRequested) { token2.ThrowIfCancellationRequested(); } else { RetrievedFiles.Add(FTPFile.Name); recordsComplete = true; } } //If there is an error, Print the error and try the file again. catch (AggregateException) { errorRepository.QuickAdd("FTPFromAllcontrollers", "Signal", "TransferFiles", ApplicationEvent.SeverityLevels.Medium, "Transfer Task Timed Out"); } catch (Exception ex) { var errorMessage = "Exception:" + ex.Message + " While Transfering file: " + FTPFile + " from " + RemoteDir + " on " + Server + " to " + LocalDir; Console.WriteLine(errorMessage); errorRepository.QuickAdd("FTPFromAllcontrollers", "Signal", "TransferFiles", ApplicationEvent.SeverityLevels.Medium, errorMessage); RetryFiles.Add(FTPFile.Name); } Thread.Sleep(waitbetweenrecords); } } } //Delete the files we downloaded. We don't want ot have to deal with the file more than once. If we delete the file form the controller once we capture it, it will reduce redundancy. if (DeleteFilesAfterFTP) { DeleteFilesFromFTPServer(FTP, RetrievedFiles, waitbetweenrecords, RemoteDir, Server); //, Token); } } catch (Exception ex) { errorRepository.QuickAdd("FTPFromAllcontrollers", "Signal", "RetrieveFiles", ApplicationEvent.SeverityLevels.Medium, Server + " " + ex.Message); Console.WriteLine(ex.Message); } FTP.Close(); FTP.Dispose(); //********************************************************** //I don't think this is doing much good. -SJ 11-22-2016 //Try to get the missing files again with a new FTP connection //if (RetryFiles.Count > 1) //{ // foreach (string FTPFile in RetryFiles) // { // try // { // Thread.Sleep(waitbetweenrecords); // FTPSClient _FTP = new FTPSClient(); // _FTP.Connect(Server, 21, Cred, SSLMode, null, null, 0, 0, 0, 6000, false, DM); // TransferFiles(_FTP, FTPFile, LocalDir, RemoteDir, Server); // if (DeleteFilesAfterFTP) // { // try // { // _FTP.DeleteFile(FTPFile); // } // catch (Exception ex) // { // Console.WriteLine("Exception:" + ex.Message + " While Deleting file: " + FTPFile + " from " + RemoteDir + " on " + Server + " to " + LocalDir); // } // } // _FTP.Close(); // _FTP.Dispose(); // } // //If there is an error, Print the error and move on. // catch (Exception ex1) // { // Console.WriteLine("Exception:" + ex1.Message + " While Transfering file: " + FTPFile + " from " + RemoteDir + " on " + Server + " to " + LocalDir); // } // } // } // RetryFiles.Clear(); //************************************** //Turn Logging off. //The ASC3 controller stoploggin if the current file is removed. to make sure logging comtinues, we must turn the loggin feature off on the //controller, then turn it back on. try { TurnOffASC3LoggingOverSNMP(SNMPRetry, SNMPPort, SNMPTimeout, Server); Thread.Sleep(SNMPTimeout); TurnOnASC3LoggingOverSNMP(SNMPRetry, SNMPPort, SNMPTimeout, Server); } catch { } } return(recordsComplete); }