How to upload files with .db format from a remote server to a local server?

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

samiarja

Guest
I am trying to create a SFTP C# application. This application will download files from a remote directory to a local server which in this case my laptop.

The application basically, read the data from a JSON file that has all the information to connect to the SFTP server, below is an example of my JSON file:


[
{
"Record": 1,
"IPaddress": "192.168.6.247",
"Machinename": "taurus",
"username": "root",
"password": "root",
"sourcefolder": "/home/root/conf",
"destfolder": "C:/Users/Sami/Desktop/DB Files/",
"filextension": "db",
"removedownloaded": 0
},
{
"Record": 2,
"IPaddress": "192.168.2.255",
"Machinename": "taurus",
"username": "root",
"password": "root",
"sourcefolder": "/home/root/conf",
"destfolder": "C:/Users/Sami/Desktop/DB Files/",
"filextension": "json",
"removedownloaded": 1
},
{
"Record": 3,
"IPaddress": "192.168.4.255",
"Machinename": "taurus",
"username": "root",
"password": "root",
"sourcefolder": "/home/root/conf",
"destfolder": "C:/Users/Sami/Desktop/DB Files/",
"filextension": "db-journal",
"removedownloaded": 1
}
]

I have tried this implementation below, it is perfectly working on files with .JSON, and .txt, but when I switch to a directory with .db files, it doesn't seem to work or download anything with (.db).

private void button6_Click_1(object sender, EventArgs e)
{
filePath = @"C:\Users\Sami\Desktop\Companies\Nautitech Mining Systems Pty Ltd\Code\JSON\app-db.json";
string text = File.ReadAllText(filePath);
var currentList = JsonConvert.DeserializeObject<List<Datalogger>>(text);

string host = currentList[0].IPaddress;
string username = currentList[0].username;
string password = currentList[0].password;
string remoteDirectory = currentList[0].sourcefolder;
string localDirectory = currentList[0].destfolder;

using (SftpClient sftp = new SftpClient(host, username, password))
{
try
{
sftp.Connect();
Console.WriteLine("Machine 1 - Connected");
var files = sftp.ListDirectory(remoteDirectory);

foreach (var file in files)
{
try
{
string remoteFileName = file.Name;
//if ((file.Name.EndsWith(".db")))

using (Stream file1 = File.OpenWrite(localDirectory + remoteFileName))
{

//if (!file.IsDirectory && !file.IsSymbolicLink)

sftp.DownloadFile(path: remoteDirectory + remoteFileName, output: file1);


}

sftp.Disconnect();
}
catch (Exception er1)
{
Console.WriteLine("An exception has been caught " + er1.ToString());
}

}
}
catch (Exception entry)
{
MessageBox.Show(entry.Message);
}
}


}

Continue reading...
 
Back
Top