使用VB.NET将文件上传到SFTP服务器 [英] Upload file to SFTP server using VB.NET

查看:132
本文介绍了使用VB.NET将文件上传到SFTP服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将文件上传到SFTP服务器.我正在使用VB.NET 2008.

I need to upload a file to SFTP server. I am using VB.NET 2008.

如何使用端口号,用户名和密码等将简单的 .csv 文件从本地计算机上传到SFTP服务器?预先感谢.

How can I upload a simple .csv file from my local computer to SFTP server using port number, user name and password, etc? Thanks in advance.

推荐答案

.NET常用的开源SFTP库是

A commonly used open source SFTP library for .NET is 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


还有其他库.如果您需要更多高级功能,例如上载目录中的所有文件甚至是完整的目录结构,您可能会发现 my WinSCP .NET程序集很有用.

使用WinSCP,您可以使用如下代码上传所有.csv文件:

With WinSCP, you can use a code like this to upload all .csv files:

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可以为上面的模板生成一个上传代码模板,用于你.

尽管如此,WinSCP .NET程序集不是本机.NET库,它只是控制台应用程序周围的.NET包装器.因此它有其自身的局限性.

这篇关于使用VB.NET将文件上传到SFTP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆