C# SFTP SSH.NET DLL 而不是 SharpSSH

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11781808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:14:27  来源:igfitidea点击:

SFTP SSH.NET DLL instead of SharpSSH

c#.netvb.netsftpsharpssh

提问by billybob

I was seeking for a free DLL for .net to handle SFTP connections.

我正在为 .net 寻找一个免费的 DLL 来处理 SFTP 连接。

I found out this project SharpSSH, but it lacks of documentation.

我发现了这个项目SharpSSH,但它缺乏文档。

I spent a lot of time to figure how the dll works. I created a test project and I started to test different functions. Some functions are working such as deleting files.

我花了很多时间来弄清楚 dll 是如何工作的。我创建了一个测试项目并开始测试不同的功能。某些功能正在运行,例如删除文件。

I have a problem with putfile() function and getfile().

我对 putfile() 函数和 getfile() 有问题。

Here an example :

这里有一个例子:

Dim ssh As SFTPUtil
ssh = New SFTPUtil("MY SERVER", "MY USER", "MY PW")
ssh.GetFile("/home/sftptest/test111.xml", "C:\text.xml")

Note that the getfile() parameters are:

请注意,getfile() 参数是:

Public Sub GetFile(remotePath As String, localPath As String)

I step in the functions, but I didn't get the correct way to pass those parameters.

我介入了这些函数,但我没有得到传递这些参数的正确方法。

I dont really know if i should use slashes(/) or backslashes (). I know that Linux uses (/)

我真的不知道我应该使用斜杠(/)还是反斜杠()。我知道 Linux 使用 (/)

I noticed for example that the "C:\" has been transformed to "C:\\".

例如,我注意到“C:\”已转换为“C:\\”。

Just to mention that the SFTP is on a linux machine.

顺便提一下,SFTP 在 linux 机器上。

thank you.

谢谢你。

采纳答案by billybob

Here's What I should have done (vb.net code) to establish the connection with THIS library (SSHnet), I did notuse SharpSHH:

这是我应该做的(vb.net代码)建立与连接这个库(SSHnet,我并没有使用SharpSHH:

Private Sub ButtonAction_Click(sender As Object, e As System.EventArgs) Handles ButtonAction.Click

    Dim PasswordConnection = New PasswordAuthenticationMethod("test", "test")
    Dim KeyboardInteractive = New KeyboardInteractiveAuthenticationMethod("test")
    Dim ConnectionInfo = New ConnectionInfo("192.168.1.1", 22, "test", PasswordConnection, KeyboardInteractive)

    AddHandler KeyboardInteractive.AuthenticationPrompt, _
    Sub(_sender As Object, _e As Renci.SshNet.Common.AuthenticationPromptEventArgs)
        For Each prompt In _e.Prompts
            Debug.Print(prompt.Request)
            If Not prompt.Request.IndexOf("Password:", StringComparison.InvariantCultureIgnoreCase) = -1 Then
                prompt.Response = "test"
            End If
        Next
    End Sub

    sftp = New SftpClient(ConnectionInfo)
    sftp.Connect()

    sftp.disconnect()
End Sub

回答by Thomas Andreè Wang

I have some problem understanding what exactly you want so here is an example code from the SSH.NET package. (NOT SharpSSH)

我在理解您到底想要什么时遇到了一些问题,所以这里是来自 SSH.NET 包的示例代码。(不是 SharpSSH)

string IP = "192.168.1.1", User = "Testuser", Pass = "123";
SftpClient sftp;

private void UploadFileSFTP()
    {
        sftp = new SftpClient(IP, User, Pass);
        sftp.Connect();
        Uploader();
        Downloader();
        sftp.Disconnect();
    }

string FilePath="C:\folder\", Filename = "Filename.extention", 
       DeliveryPath = "/tmp/";

private void Uploader()
    {
        using (var file = File.OpenRead(FilePath + Filename))
        {
            sftp.UploadFile(file, DeliveryPath + Filename);
        }
    }

//there is possibly a simpler way to download but this is how i did it.
string FromPath = "/tmp/testfile.txt", StoragePath = ""; 
private void Downloader()
    {
        if (File.Exists(StoragePath))
            File.Delete(StoragePath);
        if (!Directory.GetDirectories(Path.GetTempPath()).Contains("WorkFiles"))
        {
            Directory.CreateDirectory(Path.GetTempPath() + "WorkFiles");
        }

        StoragePath = Path.GetTempPath() + "WorkFiles\testFile.txt";

        Int64 iSize = sftp.ReadAllBytes(FromPath).Length;
        Int64 iRunningByteTotal = 0;
        using (Stream streamRemote = sftp.OpenRead(FromPath))
        {
            using (Stream streamLocal = new FileStream(StoragePath, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                int iByteSize = 0;
                byte[] byteBuffer = new byte[iSize];
                while ((iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
                {
                    streamLocal.Write(byteBuffer, 0, iByteSize);
                    iRunningByteTotal += iByteSize;
                }
            }
        }
    }

回答by opis

This library is full of bugs. I downloaded version at 11/2012 and I have big problem even with simple function like Disconnect. Application will freeze and you have to restart it.

这个库充满了错误。我在 11/2012 下载了版本,即使使用像断开连接这样的简单功能,我也遇到了大问题。应用程序将冻结,您必须重新启动它。