SFTP与.NET 3.5

我需要连接到SFTP服务器以使用C#/ .NET 3.5下载和上载文件。

.NET 3.5框架是否提供任何内置工具/机制/库来连接到SFTP服务器以下载和上载文件?

有商业解决方案:

……并且免费:

我个人对其中任何一个都没有经验。

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


Sanj的答案显示了如何通过驱动其控制台应用程序来使用WinSCP 。 虽然这确实是可能的,实际上WinSCP .NET程序集为开发人员提供了这个function,并为WinSCP脚本提供了原生.NET接口。

甚至还有一个WinSCP NuGet包 。

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

// Setup session options SessionOptions sessionOptions = new SessionOptions { Protocol = Protocol.Sftp, HostName = "example.com", UserName = "user", Password = "mypassword", SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" }; using (Session session = new Session()) { // Connect session.Open(sessionOptions); // Upload files session.PutFiles(@"d:\toupload\*", "/home/user/").Check(); } 

对于VB.NET也一样:

 ' Setup session options Dim sessionOptions As New SessionOptions With sessionOptions .Protocol = Protocol.Sftp .HostName = "example.com" .UserName = "user" .Password = "mypassword" .SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" End With Using session As New Session ' Connect session.Open(sessionOptions) ' Upload files session.PutFiles("d:\toupload\*", "/home/user/").Check() End Using 

这两种语言中都有很多其他的例子 (等等)。


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

在此处输入图像描述


如上所述,程序集只是WinSCP脚本的包装,因此它不是完全原生的.NET代码。 因此,它不适合.NET框架中的所有用例。 它主要适用于自动化任务,而不适用于开发GUI或Web应用程序。

对于完全本机的.NET SFTP库,请参阅SSH.NET ,这在任何答案中都没有提及。

(我是WinSCP的作者)

不,.NET不附带任何SFTP库。 但是,WinSCP是一个免费的实用程序,您可以下载。 我以前用它来从PayPal报表服务器下载信息。

它有一个很好的命令行设置,您也可以自动下载/上传文件。 因此,从我的.NET应用程序中,您只需使用特定参数调用该进程并等待它完成。

我更喜欢WinSCP。 它是免费的,我之前尝试过它,它完美无缺。 如果您下载COM文件,您可以执行以下操作:注意:我正在从ini传递参数。

  const string logname = "log.xml"; string username = ini.IniReadValue("sftp", "Username"); string password = ini.IniReadValue("sftp", "Password"); string remotehost = ini.IniReadValue("sftp", "Remote Host"); string dloadpath = ini.IniReadValue("Download", "Local Path"); Process winscp = new Process(); winscp.StartInfo.FileName = @ini.IniReadValue("winscp", "compath"); winscp.StartInfo.Arguments = "/log=" + logname; winscp.StartInfo.UseShellExecute = false; winscp.StartInfo.RedirectStandardInput = true; winscp.StartInfo.RedirectStandardOutput = true; winscp.StartInfo.CreateNoWindow = true; try { winscp.Start(); lblconfirm.Text = "Status: WinSCP Started Successfully"; } catch (Exception ex) { writeLog("from PutSFTP: Could not run the WinSCP executable " + winscp.StartInfo.FileName + Environment.NewLine + ex.Message); } winscp.StandardInput.WriteLine("option batch abort"); winscp.StandardInput.WriteLine("option confirm off"); winscp.StandardInput.WriteLine("open sftp://" + username + ":" + password + "@" + remotehost); winscp.StandardInput.WriteLine("cd " + ini.IniReadValue("Download", "Remote Path")); winscp.StandardInput.WriteLine(@"get " + "/" + ini.IniReadValue("Download", "Remote Path") + "/*" + ini.IniReadValue("Download", "FileType") + " " + ini.IniReadValue("Download", "Local Path")); winscp.StandardInput.Close(); string output = winscp.StandardOutput.ReadToEnd(); lblconfirm.Text = "Download Success! Check logs for moe info"; winscp.WaitForExit(); 

该框架不提供SFTP支持。

IIS 7.x (Windows服务器2008或Windows 7附带) 仅支持FTP ,而不支持本地sFTP (解释请参见下面的更新) – 但是有一些服务器解决方案可用于sFTP协议。

如果您需要sFTP客户端 ,Microsoft目前尚未提供任何支持。 但是我已经用一个名为wodSFTP.NET的第三方.NET组件做了很好的经验,这是一个sFTP客户端。

在这里您可以找到产品和文档: wodSFTP.NET 。 可以免费下载示例和在线文档,也可以从中进行注册。

请注意,您可以选择是否在C#中购买没有或带有Sourcecode的版本。 他们也有SSH组件。 在C#和VB.NET中始终可以使用示例(无论您选择哪个版本)。

示例(来自网站www.weonlydo.com,转换为C#):

 var wodSFTP1 = new WeOnlyDo.Client.SFTP(); // Authenticate with server using hostname, login, password. wodSFTP1.Hostname = "your_hostname"; wodSFTP1.Login = "your_login"; wodSFTP1.Password = "your_password"; wodSFTP1.Blocking = True; // Use synchronous connections wodSFTP1.Connect(); wodSFTP1.GetFile("c:\", "/home/somepath/somefile.txt"); 

此示例使用阻塞(同步)模式。 请注意,您也可以异步使用该组件 – 在这种情况下,将触发事件,您可以编写事件处理程序以对它们作出反应。

请注意,此示例不使用密钥进行身份validation,但当然也支持此function(您可以使用基于密钥的身份validation,基于密码的身份validation或两者都使用)。


更新: FTP和sFTP是不同的协议。 请点击链接获取解释。

由于没有本机支持,您可以在Google中搜索其他商业组件,只需输入类似’SFTP components .NET’的内容