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

huangapple go评论73阅读模式
英文:

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.
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.

英文:

I am trying to use MonoDevelop Gtk# 2.0 Project.

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

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

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 = &quot;x.x.x.x&quot;;
        string USR = &quot;user&quot;;
        string PWD = &quot;password&quot;;
        int PORT = 22;
        string L_PATH = &quot;/home/local/save/path&quot;;
        string D_PATH = &quot;/home/server/download/path&quot;;

        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 &#39;SftpClient.DownloadFile(string, Stream, Action&lt;ulong&gt;)&#39; (CS0120) (ModelManager)

How can I fix the non-static error?

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

Please help, thanks.

答案1

得分: -1

感谢 @Martin Prikryl 的帮助。

只需将 Renci.SshNet.SftpClient.DownloadFile(D_PATH, filestream) 修改为 client.DownloadFile(D_PATH, filestream)

英文:

Thanks for @Martin Prikryl's help.

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

huangapple
  • 本文由 发表于 2023年4月17日 03:20:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029892.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定