using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; //using Renci.SshNet; //using Renci.SshNet.Common; //using Renci.SshNet.Sftp; using HRM.BO; using HRM.DA; using System.IO; using System.Configuration; using Microsoft.Extensions.Configuration; using Renci.SshNet; using System.Data; using System.Data.OleDb; namespace HRM.MailNotificationProcess { public class SFTPInterface { HRM.BO.Configuration.SFTPSettings sftpsetting = new BO.Configuration.SFTPSettings(); public List DownloadFile() { // For selected File //try //{ // var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json"); // IConfiguration Configuration = builder.Build(); // Configuration.GetSection("SFTPSettings").Bind(sftpsetting); // if (sftpsetting.HostName != String.Empty) // { // using (var sftp = new SftpClient(sftpsetting.HostName, sftpsetting.Port, // sftpsetting.UserName, sftpsetting.Password)) // { // sftp.Connect(); // using (var file = File.OpenWrite(sftpsetting.LocalDestinationFilename)) // { // sftp.DownloadFile(sftpsetting.RemoteFileName, file); // } // sftp.Disconnect(); // // write in text file // // Date and Time, successfull // } // } //} List downloadedFiles = new List(); //DataTable allData = null; try { var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json"); IConfiguration Configuration = builder.Build(); Configuration.GetSection("SFTPSettings").Bind(sftpsetting); if (!string.IsNullOrEmpty(sftpsetting.HostName)) { using (var sftp = new SftpClient(sftpsetting.HostName, sftpsetting.Port, sftpsetting.UserName, sftpsetting.Password)) { sftp.Connect(); DownloadSftpDirectory(sftp, sftpsetting.RemoteFileName, sftpsetting.LocalDestinationFilename); string localDirectoryPath = sftpsetting.LocalDestinationFilename; var filesInDirectory = Directory.GetFiles(localDirectoryPath, "*.xlsx"); downloadedFiles.AddRange(filesInDirectory); sftp.Disconnect(); // write in text file // Date and Time, successfull } } } catch (Exception ex) { // write in text file // Date and Time, unsuccessfull, Error message ( 2000 character) } return downloadedFiles; } private void DownloadSftpDirectory(SftpClient sftp, string remoteDirectory, string localPath) { if (!sftp.IsConnected) throw new Exception("SFTP client not connected."); if (!Directory.Exists(localPath)) Directory.CreateDirectory(localPath); var filesAndDirs = sftp.ListDirectory(remoteDirectory); foreach (var entry in filesAndDirs) { if (entry.IsDirectory && entry.Name != "." && entry.Name != "..") { DownloadSftpDirectory(sftp, entry.FullName, Path.Combine(localPath, entry.Name)); } else if (entry.IsRegularFile) { string localFilePath = Path.Combine(localPath, entry.Name); using (var file = File.OpenWrite(localFilePath)) { sftp.DownloadFile(entry.FullName, file); } } } } public DataTable ReadExcelToDataTable(string path) { string connString = ""; string extension = System.IO.Path.GetExtension(path); if (extension == ".xlsx") { connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";"; } else if (extension == ".xls") { connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\";"; } using (OleDbConnection conn = new OleDbConnection(connString)) { conn.Open(); DataTable dtSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); string sheetName = dtSchema.Rows[0]["TABLE_NAME"].ToString(); OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + sheetName + "]", conn); OleDbDataAdapter da = new OleDbDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); return dt; } } public DataTable LoadAllExcelFilesToDataTable(string directoryPath) { DataTable aggregatedDataTable = new DataTable(); string[] excelFiles = Directory.GetFiles(directoryPath, "*.xlsx"); foreach (string filePath in excelFiles) { DataTable currentDataTable = ReadExcelToDataTable(filePath); aggregatedDataTable.Merge(currentDataTable); } return aggregatedDataTable; } } }