用于文件传输的 C# 中的 PuTTY [英] PuTTY in C# for file transfer

查看:59
本文介绍了用于文件传输的 C# 中的 PuTTY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以使用 C# 中的代码片段帮助我使用 PSCP (PuTTY) 传输方法将本地机器上的文件传输到远程服务器吗?我真的很感激你的帮助.谢谢

Can any one help me with a code snippet in C# for transferring a file on my local machine to a remote server using PSCP (PuTTY) transfer methodology? I would really appreciate the help. Thanks

推荐答案

您可以使用支持 SCP 的库,例如 SSHNetWinSCP.两者都提供示例和测试来演示它们的工作原理.

You can use a library that support SCP like SSHNet or WinSCP. Both provide samples and tests that demonstrate how they work.

使用 SSH.Net,您可以使用此代码(来自测试文件)上传文件:

With SSH.Net you can upload a file using this code (from the test files):

using (var scp = new ScpClient(host, username, password))
{
    scp.Connect();
    scp.Upload(new FileInfo(filename), Path.GetFileName(filename));
    scp.Disconnect();

}

使用 WinSCP 库,代码如下所示(来自示例):

With the WinSCP library the code looks like this (from the samples):

SessionOptions sessionOptions = new SessionOptions {
            Protocol = Protocol.Sftp,
            HostName = "example.com",
            UserName = "user",
            Password = "mypassword",
            SshHostKey = "ssh-rsa 1024 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
    TransferOptions transferOptions = new TransferOptions();
    transferOptions.TransferMode = TransferMode.Binary;

    TransferOperationResult transferResult;
    transferResult = session.PutFiles(@"d:\toupload\*", "/home/user/", false, transferOptions);

    // Throw on any error
    transferResult.Check();

}

这篇关于用于文件传输的 C# 中的 PuTTY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆