【问题标题】:download two files from SFTP site [duplicate]从SFTP站点下载两个文件[重复]
【发布时间】:2015-03-11 10:53:02
【问题描述】:

我正在使用 .NET 4.5,我需要连接到 SFTP 站点并将两个文件下载到我的本地电脑。根据我在互联网上的阅读,我没有可以在 .NET 中使用的内置库。

是否有任何我可以使用的可靠的 3rd 方也有简单的示例?

我有以下

username: myusername
password: mypassword
hostname: fts-sftp.myhost.com
protocol: SFTP
Port: 6621

更新

我有下面的代码,但是我在“sftp.Connect()”行收到以下错误消息。

System.dll 中出现“System.Net.Sockets.SocketException”类型的未处理异常

附加信息:连接尝试失败,因为连接方在一段时间后没有正确响应,或者连接失败,因为连接的主机没有响应

检查了提供给我的信用证,以确保我没有错字。

using Renci.SshNet;
using Renci.SshNet.Common;
using Renci.SshNet.Sftp;

namespace SftpExample2
{
    class Program
    {
        static void Main(string[] args)
        {

            string host = "fts-sftp.myaddress.com";
            string password = "mypassword";
            string username = "myusername";
            string remoteDirectory = ".";
            int port = 6671;

            using (SftpClient sftp = new SftpClient(host, port, username, password))
            {
                sftp.Connect();

                var files = sftp.ListDirectory(remoteDirectory);

                foreach (var file in files)
                    Console.WriteLine(file.FullName);

                sftp.Disconnect();
             };

        }
     }
 }

【问题讨论】:

    标签: c# .net ftp sftp


    【解决方案1】:

    我一般用Renci.SshNet 下面是一个下载示例,更改它以进行下载应该很简单。 我从一个旧项目中提取了它,它可能需要一些调整才能让它编译/运行

    static public void UploadFiles(string [] files)
        {
            string host = " fts-sftp.myhost.com";
            string userName = "user";
            string password = "pass";
    
            var keyboardAuthMethod = new KeyboardInteractiveAuthenticationMethod(userName);
            keyboardAuthMethod.AuthenticationPrompt += delegate(Object senderObject, AuthenticationPromptEventArgs eventArgs)
            {
                foreach (var prompt in eventArgs.Prompts)
                {
    
                    if (prompt.Request.Equals("Password: ", StringComparison.InvariantCultureIgnoreCase))
                    {
                        prompt.Response = password;
    
                    }
    
                }
    
            };
    
            var passwordAuthMethod = new PasswordAuthenticationMethod(userName, password);
            var connectInfo = new ConnectionInfo(host, userName, passwordAuthMethod, keyboardAuthMethod);
    
            using (SftpClient serverConnection = new SftpClient(connectInfo))
            {
                try
                {
    foreach (var file in files)
    {
        if (!file.Name.StartsWith("."))
        {
            string remoteFileName = file.Name;
            if (file.LastWriteTime.Date == DateTime.Today)
    
            Console.WriteLine(file.FullName);
    
            File.OpenWrite(localFileName);
    
            string sDir = @"localpath";
    
            Stream file1 = File.OpenRead(remoteDirectory + file.Name);
            sftp.DownloadFile(remoteDirectory, file1);
        }
                    serverConnection.Disconnect();
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
    
        }
    

    【讨论】:

    • 感谢您的帖子。我已经下载了 Renci.SshNet 但老实说不知道接下来要如何处理所有文件以及我实际上需要哪些文件才能下载文件?
    • 最简单的方法是在解决方案资源管理器中右键单击您的解决方案 -> 管理 nuget 包 -> 确保在左侧面板中选择在线 => nuget.org。然后在右上角的搜索栏中键入 ssh.Net 并按 Enter。然后只需单击安装,所有将被安装和配置供您的项目使用,只需在您的代码中添加使用行
    • 太好了,我现在已经在我的项目中安装和配置了它。只是我需要使用的 Renci.SshNet.Sftp 吗?
    • 欢迎来到 nuget 的神奇世界 :) 使用 Renci.SshNet;使用 Renci.SshNet.Common;
    【解决方案2】:
        using Tamir.SharpSsh;
        public void DownloadSFTP_Files()
        {
            string _ftpURL = "URLHERE";
            string _SftpUserName = "USERNAMEHERE";
            string _SftpPassword = "PASSWORDHERE";
            int _port = 22;
            Sftp oSftp = new Sftp(_ftpURL, _SftpUserName, _SftpPassword);
            oSftp.Connect(_port);
            string path = "";
            // Get List of Files in the SFTP Directory
            System.Collections.ArrayList GetFiles_List = oSftp.GetFileList(path);
            // Download the Files Form SFTP Server to you Local system
            string ServerPath= "SERVERDiRECTORYPATHHERE";
            string LocalPath= "LOCALDiRECTORYPATHHERE";
            oSftp.Get(ServerPath, LocalPath);
            oSftp.Close();
        }
    

    【讨论】:

    • 添加一些解释,说明此答案如何帮助 OP 解决当前问题
    • 试试这个代码块来访问SFTP服务器中的文件。这允许你建立到SFTP服务器的连接并下载文件。
    猜你喜欢
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多