如何在C#/ .NET中将file upload到SFTP服务器?

这个function是否带有.Net? 如果不是,最好的免费图书馆是什么? 我需要一些能够在出现问题时抛出良好例外的东西,并让我监视它的进展。

编辑:这个答案是很久以前,不再有效。 看评论

你可能想看看SharpSSH 。 它支持开箱即用的SFTP,它是开源的。

也许你可以脚本/控制winscp ?

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

以下代码显示了如何使用我们的Rebex SFTP组件将file upload到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(); 

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

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

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

 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); } 

欲了解更多信息,请参阅教程或下载试用版,并检查样品 。

.net框架中没有解决scheme。

http://www.eldos.com/sbb/sftpcompare.php列出了一些非免费的选项。;

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

不幸的是,它不在.NET框架本身。 我的愿望是,你可以集成FileZilla,但我不认为它暴露了一个接口。 他们确实有脚本我觉得,但它不会显得那么干净。

我已经在一个SFTP项目中使用CuteFTP。 它公开了一个COM组件,我创build了一个.NET包装器。 抓住,你会发现,是权限。 它在安装了CuteFTP的Windows凭据下运行得非常漂亮,但在其他凭据下运行需要在DCOM中设置权限。

对于另一个免费的选项尝试edtFTPnet / PRO 。 它具有对SFTP的全面支持,并且还支持FTPS(当然还有FTP)。