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

  • Question

  • 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);
                    }
                }
    
              
            }



    • Edited by samiarja Monday, August 5, 2019 4:26 AM
    Monday, August 5, 2019 4:26 AM

Answers

  • Hi samiarja,

    Thank you for posting here.

    Based on your description, you want to download .db file successfully from sftp server.

    I have modified the code you provided, it works well.

    using (SftpClient sftp = new SftpClient(host, username, password)) { try { sftp.Connect(); //MessageBox.Show("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(Path.Combine(localDirectory,remoteFileName))) { string path = remoteDirectory + "/" + remoteFileName; sftp.DownloadFile(path, file1); } } catch (Exception er1) { MessageBox.Show("An exception has been caught " + er1.ToString()); } } } catch (Exception entry) { MessageBox.Show(entry.Message); } finally { sftp.Disconnect(); } }

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by samiarja Tuesday, August 6, 2019 5:48 AM
    Tuesday, August 6, 2019 2:53 AM
    Moderator
  • Hi samiarja,

    Thanks for the feedback.

    This error tells you that you need some permissions to do it.

    >>Is this from the remote folder or my device?

    It comes from your local device. Therefore, I suggest that you could set the download location to D drive instead of C drive.

    After changing this, please use the code I provided to see if it works.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by samiarja Tuesday, August 6, 2019 5:59 AM
    Tuesday, August 6, 2019 5:18 AM
    Moderator

All replies

  • Hi samiarja,

    Thank you for posting here.

    Based on your description, you want to download .db file successfully from sftp server.

    I have modified the code you provided, it works well.

    using (SftpClient sftp = new SftpClient(host, username, password)) { try { sftp.Connect(); //MessageBox.Show("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(Path.Combine(localDirectory,remoteFileName))) { string path = remoteDirectory + "/" + remoteFileName; sftp.DownloadFile(path, file1); } } catch (Exception er1) { MessageBox.Show("An exception has been caught " + er1.ToString()); } } } catch (Exception entry) { MessageBox.Show(entry.Message); } finally { sftp.Disconnect(); } }

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by samiarja Tuesday, August 6, 2019 5:48 AM
    Tuesday, August 6, 2019 2:53 AM
    Moderator
  • Hi Jack,

    Thanks for your solution, however, I am encountering this error only for .db files. 

    Exception thrown: 'System.NotSupportedException' in mscorlib.dll
    Exception thrown: 'System.UnauthorizedAccessException' in mscorlib.dll

    and

    An exception has been caught System.UnauthorizedAccessException: Access to the path 'C:\Users\Sami\Desktop\DB Files\new data' is denied.....

    Is this from the remote folder or my device?

    I have tried it for .json and .txt and it perfectly works.

    Tuesday, August 6, 2019 5:06 AM
  • Hi samiarja,

    Thanks for the feedback.

    This error tells you that you need some permissions to do it.

    >>Is this from the remote folder or my device?

    It comes from your local device. Therefore, I suggest that you could set the download location to D drive instead of C drive.

    After changing this, please use the code I provided to see if it works.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by samiarja Tuesday, August 6, 2019 5:59 AM
    Tuesday, August 6, 2019 5:18 AM
    Moderator
  • Hi Jack, Thanks a lot that solved my problem for some folders and I also found out that I don't have access permission for one entire folder in the remote server, as they are locked by the administrator, but this will be solved soon as well.

    Thanks again.


     
    Tuesday, August 6, 2019 5:48 AM