Search code examples
c#sshsftpmonodevelopgtk#

MonoDevelop Gtk# 2.0 Project cannot use non-static DownloadFile from SFTP with SSH.NET library


I am trying to use MonoDevelop Gtk# 2.0 Project.

And I want to use DownloadFile from SFTP with SSH.NET library.

enter image description here

If I click the Download button, I can download the selected file from a remote server to my local device.

The following is my code.

(I use GUI Designer to create my button.)

using System;
using Gtk;

public partial class MainWindow : Gtk.Window
{
    public MainWindow() : base(Gtk.WindowType.Toplevel)
    {
        Build();
    }

    protected void OnDeleteEvent(object sender, DeleteEventArgs a)
    {
        Application.Quit();
        a.RetVal = true;
    }

    protected void DownloadModel(object sender, EventArgs e)
    {
        string HOST = "x.x.x.x";
        string USR = "user";
        string PWD = "password";
        int PORT = 22;
        string L_PATH = "/home/local/save/path";
        string D_PATH = "/home/server/download/path";

        var client = new Renci.SshNet.SftpClient(HOST, PORT, USR, PWD);
        client.Connect();

        if(client.IsConnected)
        {
            using (System.IO.Stream filestream = System.IO.File.Create(L_PATH))
            {
                Renci.SshNet.SftpClient.DownloadFile(D_PATH, filestream);               
            }
        }
    }
}

But the compile error happens below.

/data/user/C#/ModelManager/ModelManager/MainWindow.cs(21,21): Error CS0120: An object reference is required for the non-static field, method, or property 'SftpClient.DownloadFile(string, Stream, Action<ulong>)' (CS0120) (ModelManager)

How can I fix the non-static error?

Or is there any other method to download file in MonoDevelop?

Please help, thanks.


Solution

  • Thanks for @Martin Prikryl's help.

    Just revise Renci.SshNet.SftpClient.DownloadFile(D_PATH, filestream) to client.DownloadFile(D_PATH, filestream).