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

Cor*_*rey 67 .net c# sftp

是否存在可以将文件上载到SFTP(SSH FTP)服务器的免费.NET库,该服务器会抛出上载问题的异常并允许监视其进度?

小智 37

也许你可以脚本/控制winscp

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

  • [最新版本](http://winscp.net/eng/docs/library)现在甚至具有本机.NET支持! (11认同)
  • 好点子.我找到了这样做的指南:http://winscp.net/eng/docs/guide_dotnet (4认同)

Mar*_*obr 8

以下代码显示了如何使用我们的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();
Run Code Online (Sandbox Code Playgroud)

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

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

或使用以下事件拦截通信:

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);
}
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅教程下载试用版并检查样本.

  • 当我需要将它用于SSIS包时,我之前也使用过RebEx.如果您希望通过商业第三方路线,他们有良好的支持和文档. (2认同)

ddc*_*660 2

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

http://www.eldos.com/sbb/sftpcompare.php概述了非自由选项的列表。

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

  • Granados 看起来不能在生产环境中使用:它已经过时并且似乎不受支持。 (2认同)