C# 如何与SFTP服务器通信

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1881122/
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-06 21:36:49  来源:igfitidea点击:

How to communicate with SFTP server

c#.netsftp

提问by Thorsten Dittmar

I've written a service for our customer that automatically transmits files to given destinations using FTP. For historic reasons I'm using WinInet to perform the FTPing. All works well, but now the customer wants to add one destination that only accepts SFTP connections.

我为我们的客户编写了一项服务,该服务使用 FTP 自动将文件传输到给定的目的地。由于历史原因,我使用 WinInet 来执行 FTPing。一切正常,但现在客户想要添加一个仅接受 SFTP 连接的目的地。

I do not really like the idea of implementing this from scratch, so is there a way to communicate natively or through WinInet with a SFTP server? Are there any system libraries that I can use (I have no fear of P/Invoke :) )? Do I have to purchase third party components for that - if so, which ones would you suggest?

我真的不喜欢从头开始实现这个想法,所以有没有办法在本地或通过 WinInet 与 SFTP 服务器进行通信?有没有我可以使用的系统库(我不怕 P/Invoke :))?我是否必须为此购买第三方组件 - 如果是这样,您会建议使用哪些组件?

采纳答案by mdb

Unfortunately, SFTP is not natively supported by WinInet or any other standard Windows libraries.

不幸的是,WinInet 或任何其他标准 Windows 库本身并不支持 SFTP。

I've had good luck with /n Software's IP*Works SSH for .NETin talking to a large variety of SFTP servers.

我很幸运使用 /n Software 的IP*Works SSH for .NET与各种 SFTP 服务器通信。

回答by davewasthere

http://sourceforge.net/projects/sharpssh/could be your best best. I ended up writing scripts for WinSCP though, which was easier...

http://sourceforge.net/projects/sharpssh/可能是您最好的选择。不过,我最终还是为 WinSCP 编写了脚本,这更容易……

EDITfor clarity, I'd recommend something like https://github.com/sshnet/SSH.NETnow as SharpSSH is a dead project

为了清楚起见编辑,我现在推荐像https://github.com/sshnet/SSH.NET这样的东西,因为 SharpSSH 是一个死项目

回答by Martin Vobr

the .NET Framwork supports FTP via the FtpWebRequest since version 2.0. No support for SFTP yet.

.NET 框架从 2.0 版开始通过 FtpWebRequest 支持 FTP。尚不支持 SFTP。

If you need both FTP and SFTP you have to try a third party component. There is an open source implementation of SFTPfrom Tamir Gal which is often recommended. Or you can try one of commercial SFTP components such as our Rebex SFTP.

如果您同时需要 FTP 和 SFTP,则必须尝试第三方组件。Tamir Gal有一个开源的 SFTP 实现,经常被推荐。或者,您可以尝试使用一种商业 SFTP 组件,例如我们的Rebex SFTP

Following code is taken from Rebex SFTP tutorial page:

以下代码取自Rebex SFTP 教程页面

// create client, connect and log in 
Sftp client = new Sftp();
client.Connect(hostname);
client.Login(username, password);

// upload the content of 'c:\data' directory and all subdirectories 
// to the '/wwwroot' directory at the server 
client.PutFiles(@"c:\data\*", "/wwwroot", SftpBatchTransferOptions.Recursive);

client.Disconnect();

If you are not sure whether you will need FTPS or SFTP you can check the Rebex File Transfer Packwhich includes both. The FTP API is almost identical to the SFTP one.

如果您不确定是否需要 FTPS 或 SFTP,您可以查看包含两者的Rebex 文件传输包。FTP API 几乎与 SFTP API 相同。

回答by Eugene Mayevski 'Callback

JFYI: probably the most feature-rich components for SFTP in .NET is our SFTPBlackbox.

JFYI: .NET 中 SFTP 功能最丰富的组件可能是我们的SFTPBlackbox

回答by Martin Prikryl

There's no support for SFTP in .NET framework, in any version.

在任何版本的 .NET 框架中都不支持 SFTP。



You have to use a third party library for SFTP.

您必须为 SFTP 使用第三方库。

You can use WinSCP .NET assembly. There's even a WinSCP NuGet package.

您可以使用WinSCP .NET 程序集。甚至还有一个WinSCP NuGet 包

A trivial SFTP upload C# example:

一个简单的 SFTP 上传 C# 示例:

// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Sftp,
    HostName = "example.com",
    UserName = "user",
    Password = "mypassword",
    SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx...="
};

using (Session session = new Session())
{
    // Connect
    session.Open(sessionOptions);

    // Upload files
    session.PutFiles(@"d:\toupload\*", "/home/user/").Check();
}

There are lot of other examples.

还有很多其他的例子



You can have WinSCP GUI generate an SFTP code template, like above, for you, including C#, VB.NET and PowerShell.

您可以让WinSCP GUI为您生成一个 SFTP 代码模板,如上所示,包括 C#、VB.NET 和 PowerShell。

enter image description here

在此处输入图片说明



The assembly is just a wrapper around WinSCP scripting, so it's not a completely native .NET code. As such it does not fit all use cases in .NET framework. It is mostly suitable for automating tasks, somewhat for developing GUI applications, and not really for web applications.

该程序集只是 WinSCP 脚本的包装器,因此它不是完全本机的 .NET 代码。因此,它并不适合 .NET 框架中的所有用例。它主要适用于自动化任务,有点适用于开发 GUI 应用程序,而不是真正适用于 Web 应用程序。

For a fully native .NET SFTP library, see SSH.NET, which is strangely not mentioned in any answer yet.

对于完全本机的 .NET SFTP 库,请参阅SSH.NET,奇怪的是尚未在任何答案中提及。

(I'm the author of WinSCP)

(我是 WinSCP 的作者)

回答by Duc Filan

I created a demo of interactions with SFTP server using WinSCP.

我创建了一个使用 WinSCP 与 SFTP 服务器交互的演示。

This application is able to upload, delete, rename and fetch info of files system in an SFTP Server using WinSCP .NET Assembly.

此应用程序能够使用 WinSCP .NET 程序集上传、删除、重命名和获取 SFTP 服务器中文件系统的信息。

Take a look at: https://github.com/ducfilan/SFTP-with-WinSCP

看一看:https: //github.com/ducfilan/SFTP-with-WinSCP