SFTP 与 .NET 3.5

发布于 2024-08-31 09:42:55 字数 101 浏览 6 评论 0原文

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

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

I need to connect to an SFTP server to download and upload files using C#/.NET 3.5.

Does the .NET 3.5 framework provide any built-in tools/mechanisms/libraries to connect to an SFTP server to download and upload files?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

缱绻入梦 2024-09-07 09:42:55

.NET Framework 的任何版本都不支持 SFTP。


Sanj 的回答展示了如何通过驱动其控制台应用程序来使用 WinSCP。虽然这确实是可能的,但实际上 WinSCP .NET 程序集 为开发人员完成了这件事,给了他/她的 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 xxxxxxxxxxx...="
};

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 xxxxxxxxxxx...="
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 的作者)

There's no support for SFTP in .NET framework, in any version.


The answer by Sanj shows how to use WinSCP by driving its console application. While that's indeed possible, where actually WinSCP .NET assembly that does it for a developer, giving him/her native .NET interface to the WinSCP scripting.

There's even a WinSCP NuGet package.

A trivial SFTP upload C# example:

// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Sftp,
    HostName = "example.com",
    UserName = "user",
    Password = "mypassword",
    SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx...="
};

using (Session session = new Session())
{
    // Connect
    session.Open(sessionOptions);

    // Upload files
    session.PutFiles(@"d:\toupload\*", "/home/user/").Check();
}

And the same for 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 xxxxxxxxxxx...="
End With

Using session As New Session
    ' Connect
    session.Open(sessionOptions)

    ' Upload files
    session.PutFiles("d:\toupload\*", "/home/user/").Check()
End Using

There are lot of other examples in both languages (and more).


You can have WinSCP GUI generate an SFTP code template, like above, for you, including C#, VB.NET and PowerShell.

enter image description here


As mentioned above, the assembly is just a wrapper around WinSCP scripting, so it's not a completely native .NET code. As such it does not fit all use cases in .NET framework. It is mostly suitable for automating tasks, not for developing GUI or web applications.

For a fully native .NET SFTP library, see SSH.NET, which is strangely not mentioned in any answer yet.

(I'm the author of WinSCP)

勿挽旧人 2024-09-07 09:42:55

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

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

No, .NET doesn't ship with any SFTP libraries. However, WinSCP is a free utility you can download. I've used it before to download information from a PayPal report server.

It has a nice command line setup for you to automate the download / upload of files too. So from my .NET app, you just invoke the process with the specific arguments and wait until it completes.

一笔一画续写前缘 2024-09-07 09:42:55

我更喜欢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();

I prefer WinSCP. It's free and I've tried it before, it works perfectly. If you download the COM file and you could do something like below:
NOTE: I'm passing the params from 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();
荒岛晴空 2024-09-07 09:42:55

该框架不提供 SFTP 支持。

The framework does not provide SFTP support.

奢欲 2024-09-07 09:42:55

IIS 7.x(随 Windows Server 2008 或 Windows 7 一起提供)支持 仅限 FTP,而非 sFTP 本地(解释请参阅下面的更新) - 但是有像这个这样的服务器解决方案可用对于 sFTP 协议。

如果您需要sFTP 客户端,Microsoft 迄今为止尚未提供任何支持。但我在使用名为 wodSFTP.NET 的第 3 方 .NET 组件(一个 sFTP 客户端)方面取得了很好的经验。

在这里您可以找到产品和信息文档:wodSFTP.NET
示例和在线文档可以免费下载,也无需注册。

请注意,您可以选择购买不带或带 C# 源代码的版本。他们也有 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");

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

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


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

IIS 7.x (which comes with Windows server 2008 or Windows 7) supports only FTPs, not sFTP natively (explanation see update below) - however there are server solutions like this one available for the sFTP protocol.

If you require an sFTP client, Microsoft has not provided any support so far. But I've made very good expericence with a 3rd party .NET component named wodSFTP.NET, which is a sFTP client.

Here you can find the product & documentation: wodSFTP.NET.
Examples and online-documentation can be downloaded without any charge or registration from them.

Note that you can choose whether you buy the version without or with Sourcecode in C#. They have a SSH component as well. Examples are always available (regardless of the version you choose) in C# and VB.NET.

Example (from the website www.weonlydo.com, converted to 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");

This example uses blocking (synchronous) mode. Note that you can also use the component asynchronously - in this case events will be fired and you can write event handlers to react on them.

Note that this example does not use a key to authenticate, but of course this is also supported (you can use key-based authentication, password-based authentication or both).


Update: FTPs and sFTP are different protocols. Please follow the links for an explanation.

ゞ花落谁相伴 2024-09-07 09:42:55

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

Since there is no native support, uou can search in Google for other, commercial, components, just type something like 'SFTP components .NET'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文