如何在 C# (.NET) 中将文件上传到 SFTP 服务器?

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

How do I upload a file to an SFTP server in C# (.NET)?

提问by Corey

Does a free .NET library exist with which I can upload a file to a SFTP (SSH FTP) server, which throws exceptions on problems with the upload and allows the monitoring of its progress?

是否存在免费的 .NET 库,我可以使用它将文件上传到 SFTP(SSH FTP)服务器,该服务器会在上传问题时引发异常并允许监控其进度?

回答by ddc0660

There is no solution for this within the .net framework.

.net 框架内没有解决方案。

http://www.eldos.com/sbb/sftpcompare.phpoutlines a list of un-free options.

http://www.eldos.com/sbb/sftpcompare.php概述了非免费选项列表。

your best free bet is to extend SSH using Granados. http://www.routrek.co.jp/en/product/varaterm/granados.html

最好的免费选择是使用 Granados 扩展 SSH。http://www.routrek.co.jp/en/product/varaterm/granados.html

回答by Mike L

Unfortunately, it's not in the .NET Framework itself. My wish is that you could integrate with FileZilla, but I don't think it exposes an interface. They do have scripting I think, but it won't be as clean obviously.

不幸的是,它不在 .NET Framework 中。我的愿望是您可以与 FileZilla 集成,但我认为它没有公开接口。我认为他们确实有脚本,但显然不会那么干净。

I've used CuteFTP in a project which does SFTP. It exposes a COM component which I created a .NET wrapper around. The catch, you'll find, is permissions. It runs beautifully under the Windows credentials which installed CuteFTP, but running under other credentials requires permissions to be set in DCOM.

我在一个执行 SFTP 的项目中使用了 CuteFTP。它公开了一个 COM 组件,我在该组件周围创建了一个 .NET 包装器。您会发现,问题在于权限。它在安装了CuteFTP 的Windows 凭据下运行良好,但在其他凭据下运行需要在DCOM 中设置权限。

回答by Kasprzol

Maybe you can script/control winscp?

也许您可以编写/控制winscp

Update:winscp now has a .NET libraryavailable as a nuget packagethat supports SFTP, SCP, and FTPS

更新:winscp 现在有一个 .NET 库可用作支持 SFTP、SCP 和 FTPS的nuget 包

回答by Bruce Blackshaw

For another un-free option try edtFTPnet/PRO. It has comprehensive support for SFTP, and also supports FTPS (and of course FTP) if required.

对于另一个非免费选项,请尝试edtFTPnet/PRO。它对 SFTP 有全面的支持,如果需要,还支持 FTPS(当然还有 FTP)。

回答by Martin Vobr

Following code shows how to upload a file to a SFTP server using our Rebex SFTPcomponent.

以下代码显示了如何使用我们的Rebex SFTP组件将文件上传到 SFTP 服务器。

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

// upload the 'test.zip' file to the current directory at the server 
client.PutFile(@"c:\data\test.zip", "test.zip");

client.Disconnect();

You can write a complete communication log to a file using a LogWriter property as follows. Examples output (from FTP component but the SFTP output is similar) can be found here.

您可以使用 LogWriter 属性将完整的通信日志写入文件,如下所示。示例输出(来自 FTP 组件,但 SFTP 输出类似)可以在这里找到。

client.LogWriter = new Rebex.FileLogWriter(
   @"c:\temp\log.txt", Rebex.LogLevel.Debug); 

or intercept the communication using events as follows:

或使用事件拦截通信,如下所示:

Sftp client = new Sftp();
client.CommandSent += new SftpCommandSentEventHandler(client_CommandSent);
client.ResponseRead += new SftpResponseReadEventHandler(client_ResponseRead);
client.Connect("sftp.example.org");

//... 
private void client_CommandSent(object sender, SftpCommandSentEventArgs e)
{
    Console.WriteLine("Command: {0}", e.Command);
}

private void client_ResponseRead(object sender, SftpResponseReadEventArgs e)
{
    Console.WriteLine("Response: {0}", e.Response);
}

For more info see tutorialor downloada trial and check samples.

有关更多信息,请参阅教程下载试用版并检查示例