简体   繁体   English

使用VB.NET将文件上传到SFTP服务器

[英]Upload file to SFTP server using VB.NET

I need to upload a file to SFTP server. 我需要将文件上传到SFTP服务器。 I am using VB.NET 2008. 我正在使用VB.NET 2008。

How can I upload a simple .csv file from my local computer to SFTP server using port number, user name and password, etc? 如何使用端口号,用户名和密码等将简单的.csv文件从本地计算机上传到SFTP服务器? Thanks in advance. 提前致谢。

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 my WinSCP .NET assembly useful. 如果您需要更多高级功能,例如上传目录中的所有文件甚至完整的目录结构,您可能会发现我的 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. 虽然,WinSCP .NET程序集不是本机.NET库,但它只是一个围绕控制台应用程序的.NET包装器。 So it has its own limitations. 所以它有其自身的局限性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM