Unable to download files with .db format from remote server to local server?

  • Thread starter Thread starter samiarja
  • Start date Start date
S

samiarja

Guest
Hi all,

I developed an application that takes user input and store all the values in a structured .JSON file as shown below:

[
{
"Record": 1,
"IPaddress": "192.168.6.67",
"Machinename": "naut",
"username": "net",
"password": "net",
"sourcefolder": "/home/root/data/completed",
"destfolder": "C:\\temp",
"filextension": ".db",
"removedownloaded": 0,
"removecsv": 0,
"removedb": 0,
"eagleIOHost": "sftp.eagle.io",
"eagleIOUsername": "sftp.eagle.io",
"eagleIOpassword": "nautitech250",
"eagleIOdirectory": "/usr/share/tomcat8"
}
]

these value are then used to connect to the local server and download data to the local server and then convert all the file to .csv which will then be visualized.

However, the file that are recorded seem to be empty and I am getting exception error. The code is below:

First read the .JSON file and deserialize it and convert it to a list

string text = File.ReadAllText(filePath);
var currentList = JsonConvert.DeserializeObject<List<Datalogger>>(text);

then read all the value on the JSON

string host = item.IPaddress;
string username = item.username;
string password = item.password;
string remoteDirectory = item.sourcefolder;
string localDirectory = item.destfolder;
string filextension = item.filextension;
string removedownloaded = item.removedownloaded.ToString();
string removecsv = item.removecsv.ToString();
string removedb = item.removedb.ToString();

string host_eagleio = item.eagleIOHost;
string username_eagleio = item.eagleIOUsername;
string password_eagleio = item.eagleIOpassword;
string eagleIOdirectoryDB = item.eagleIOdirectory;



Then connect to SFTP server then iterate through the files in the remote server and replace any occurrences of ':' by '_' (DADLoggerDB_2000-01-13T02:59:58 ==> DADLoggerDB_2000-01-13T02_59_58)

2 files should be created in the local server: one for the .db files and one for .csv files after conversion. Finally, the CSV file will be sent to the cloud server for data visualization.

using (SftpClient sftp = new SftpClient(host, username, password))
{
try
{
sftp.Connect();
var files = sftp.ListDirectory(remoteDirectory);

//Parallel.ForEach(files, file =>
foreach (var file in files)
{
try
{

string remoteFileName = file.Name;

string str = "";
str = remoteFileName.Replace(":", "_");
Console.WriteLine(str);
string Folder_path = item.destfolder;
var files_path = Directory.GetFiles(Folder_path, "*db", SearchOption.AllDirectories);
System.IO.DirectoryInfo local_directory = new DirectoryInfo(Folder_path);
System.IO.DirectoryInfo local_directory_csv = new DirectoryInfo(Folder_path);
string tablename = string.Empty;
//string path = remoteDirectory + "/" + remoteFileName;
string path = remoteDirectory + "/" + remoteFileName;
if ((file.Name.EndsWith(".db")))
{
string subFolder_raw = Path.Combine(localDirectory, "Data");
string subFolder = Path.Combine(localDirectory, "CSV");
using (Stream file1 = File.OpenWrite(Path.Combine(subFolder_raw, remoteFileName)))
{


sftp.DownloadFile(path, file1);
foreach (var item_db in files_path)
{

var result = item_db.Substring(item_db.Length - 3);
for (var i = 1; i < files_path.Count() + 1; i++)
{
if (result == ".db")
{

tablename = string.Format("DADLoggerTable{0}", i);
DataTable dt = ConverttoDatatable(item_db, tablename);
SaveCsv(dt, subFolder, tablename);
}
}
}

using (var sftp_eagleio = new SftpClient(host_eagleio, username_eagleio, password_eagleio))
{
sftp_eagleio.Connect();
var files_eagleio = Directory.GetFiles(subFolder);
foreach (var file_eagleio in files_eagleio)
{
string remoteFileName_eagleio = file_eagleio;
using (Stream file1_eagleio = new FileStream(remoteFileName_eagleio, FileMode.Open))
{
remoteFileName_eagleio = remoteFileName_eagleio.Substring(15);
sftp.UploadFile(file1_eagleio, eagleIOdirectoryDB + "/" + remoteFileName_eagleio, null);
}
}
}


}
if (removedownloaded == "1")
{
sftp.Delete(path);

}

foreach (FileInfo filedb in local_directory.EnumerateFiles())
{
if (removedb == "1")
{
filedb.Delete();
}
}
foreach (FileInfo filecsv in local_directory_csv.EnumerateFiles())
{
if (removecsv == "1")
{
filecsv.Delete();
}
}
}

}
catch (Exception er1)
{
MessageBox.Show("An exception has been caught " + er1.ToString());
}}
//});
}
catch (Exception entry)
{
MessageBox.Show(entry.Message);
}
}



However, this bit of code is causing a problem

string remoteFileName = file.Name;
string str = "";
str = remoteFileName.Replace(":", "_");

when ":" is replaced by "_", I encountered this error after sftp.DownloadFile(path, file1); being executed:

A first chance exception of type 'Renci.SshNet.Common.SftpPathNotFoundException' occurred in Renci.SshNet.dll

and when I comment out this line and directly download the file without this replacement operation, I encounter this error after sftp.DownloadFile(path, file1); being executed::

A first chance exception of type 'System.NotSupportedException' occurred in mscorlib.dll

I tried different local repository rather than C drive, but still it is not working.

Any suggestion?

Thanks in advance

Sami






Sami Arja

Continue reading...
 
Back
Top