How to communicate with SFTP server

后端 未结 7 1516
谎友^
谎友^ 2021-02-14 01:48

I\'ve written a service for our customer that automatically transmits files to given destinations using FTP. For historic reasons I\'m using WinInet to perform the FTPing. All w

相关标签:
7条回答
  • 2021-02-14 01:51

    I created a demo of interactions with SFTP server using WinSCP.

    This application is able to upload, delete, rename and fetch info of files system in an SFTP Server using WinSCP .NET Assembly.

    Take a look at: https://github.com/ducfilan/SFTP-with-WinSCP

    0 讨论(0)
  • 2021-02-14 01:54

    JFYI: probably the most feature-rich components for SFTP in .NET is our SFTPBlackbox.

    0 讨论(0)
  • 2021-02-14 02:02

    Unfortunately, SFTP is not natively supported by WinInet or any other standard Windows libraries.

    I've had good luck with /n Software's IP*Works SSH for .NET in talking to a large variety of SFTP servers.

    0 讨论(0)
  • 2021-02-14 02:04

    http://sourceforge.net/projects/sharpssh/ could be your best best. I ended up writing scripts for WinSCP though, which was easier...

    EDIT for clarity, I'd recommend something like https://github.com/sshnet/SSH.NET now as SharpSSH is a dead project

    0 讨论(0)
  • 2021-02-14 02:13

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


    You have to use a third party library for SFTP.

    You can use WinSCP .NET assembly. 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();
    }
    

    There are lot of other examples.


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


    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, somewhat for developing GUI applications, and not really for 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)


    Windows 10 also come with command-line OpenSSH sftp client. It can also be downloaded for older versions of Windows.

    0 讨论(0)
  • 2021-02-14 02:13

    the .NET Framwork supports FTP via the FtpWebRequest since version 2.0. No support for SFTP yet.

    If you need both FTP and SFTP you have to try a third party component. There is an open source implementation of SFTP from Tamir Gal which is often recommended. Or you can try one of commercial SFTP components such as our Rebex SFTP.

    Following code is taken from Rebex SFTP tutorial page:

    // create client, connect and log in 
    Sftp client = new Sftp();
    client.Connect(hostname);
    client.Login(username, password);
    
    // upload the content of 'c:\data' directory and all subdirectories 
    // to the '/wwwroot' directory at the server 
    client.PutFiles(@"c:\data\*", "/wwwroot", SftpBatchTransferOptions.Recursive);
    
    client.Disconnect();
    

    If you are not sure whether you will need FTPS or SFTP you can check the Rebex File Transfer Pack which includes both. The FTP API is almost identical to the SFTP one.

    0 讨论(0)
提交回复
热议问题