使用 VB.NET 上传文件到 SFTP 服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48556236/
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
Upload file to SFTP server using VB.NET
提问by Farhan Ahmed Saifi
I need to upload a file to SFTP server. I am using VB.NET 2008.
我需要将文件上传到 SFTP 服务器。我正在使用 VB.NET 2008。
How can I upload a simple .csvfile from my local computer to SFTP server using port number, user name and password, etc? Thanks in advance.
如何.csv使用端口号、用户名和密码等将简单文件从本地计算机上传到 SFTP 服务器?提前致谢。
采纳答案by Martin Prikryl
A commonly used open source SFTP library for .NET is SSH.NET.
.NET 的常用开源 SFTP 库是SSH.NET。
With it, you can use a code like this:
有了它,你可以使用这样的代码:
Dim client As SftpClient = New SftpClient("example.com", "username", "password")
client.Connect()
Using stream As Stream = File.OpenRead("C:\local\path\some.csv")
client.UploadFile(stream, "/remote/path/some.csv")
End Using
There are other libraries too. If you need more high-level functions, like uploading all files in a directory or even complete directory structures, you may find myWinSCP .NET assemblyuseful.
还有其他图书馆。如果您需要更多高级功能,例如上传目录中的所有文件甚至完整的目录结构,您可能会发现我的WinSCP .NET 程序集很有用。
With WinSCP, you can use a code like this to upload all .csv files:
使用 WinSCP,您可以使用这样的代码上传所有 .csv 文件:
Dim sessionOptions As New SessionOptions
With sessionOptions
.Protocol = Protocol.Sftp
.HostName = "example.com"
.UserName = "username"
.UserName = "password"
.SshHostKeyFingerprint = "ssh-rsa 2048 ..."
End With
Using session As New Session
session.Open(sessionOptions)
session.PutFiles("C:\local\path\*.csv", "/remote/path/*").Check()
End Using
WinSCP GUI can generate an upload code template, like the one above, for you.
WinSCP GUI 可以为您生成一个上传代码模板,就像上面的那个一样。
Though, WinSCP .NET assembly is not a native .NET library, it's just a .NET wrapper around a console application. So it has its own limitations.
不过,WinSCP .NET 程序集不是本机 .NET 库,它只是控制台应用程序的 .NET 包装器。所以它有自己的局限性。

