英文:
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.
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.
答案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)
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论