Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将文件上载到C#(.NET)中的SFTP服务器?_C#_.net_Sftp - Fatal编程技术网

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

如何将文件上载到C#(.NET)中的SFTP服务器?,c#,.net,sftp,C#,.net,Sftp,是否存在一个免费的.NET库,我可以使用它将文件上载到SFTP(SSH FTP)服务器,该服务器会对上载问题抛出异常,并允许监视其进度?在.NET framework中没有解决方案 概述了一个非免费选项列表 您最好的免费选择是使用dos扩展SSH 不幸的是,它不在.NET框架中。我的愿望是您可以与FileZilla集成,但我认为它不会公开接口。我想他们确实有脚本,但显然不会那么干净 我在一个做SFTP的项目中使用了CuteFTP。它公开了一个COM组件,我在其周围创建了一个.NET包装器。你会发

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

在.NET framework中没有解决方案

概述了一个非免费选项列表


您最好的免费选择是使用dos扩展SSH

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


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

也许您可以编写脚本/控制


更新:winscp现在作为一个支持SFTP、SCP和FTP的组件提供了另一个un免费选项,请尝试。它对SFTP有全面的支持,如果需要,还支持FTPS(当然还有FTP)。

下面的代码展示了如何使用我们的组件将文件上传到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输出类似)

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

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 SFTP客户端的有限测试表明,此商业产品是不错的选择。我以前也曾在需要将RebEx产品用于SSIS包时使用过它。如果你想通过商业第三方渠道,他们有很好的支持和文档。这一点很好。我找到了一本指南来指导如何做到这一点:现在甚至有原生的.NET支持@RonnieOverby,你的意思是WinSCP原生.net版本有这个带有绝对路径的错误吗?看起来Granados不能在生产环境中使用:它已经过时,似乎不受支持。编辑:这个答案是很久以前的,不再有效。请参阅您可能想看一看的评论。它支持开箱即用的SFTP,并且是开源的。另请参阅问题和另一个备选方案是Renci SSH.NET。
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);
}