Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 965315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:58:36+00:00 2026-05-16T01:58:36+00:00

I’m trying to use SharpSSH to get a file from a remote SFTP server,

  • 0

I’m trying to use SharpSSH to get a file from a remote SFTP server, and I want to read it out as a stream.

I found:

  • class Sftp, which has a Get method that saves it to a local file — close

  • class SshStream, which might do kind of what I want, but seems disjoint from Sftp so I might have to implement the SFTP part myself (??)

  • class ChannelSftp, which implements SFTP methods like get(String, OutputStream), which seems perfect, except it’s a low-level class and it’s not at all obvious to me how to even instantiate it

It looks like if Sftp‘s ChannelSftp SftpChannel property wasn’t private, I could use that and everything would be perfect. I’d like to avoid hacking up SharpSSH if possible, though.

Am I missing something?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:58:37+00:00Added an answer on May 16, 2026 at 1:58 am

    I worked something out, and tested it out. Give it a try, and feel free to massage the API.

    First off, you will need to surface up a method that allows you to take advantage of the ChannelSftp methods that call for OutputStreams instead of destination file names. If you don’t want to use reflection to do it, then add this method to the Sftp class and recompile SharpSSH.

    public void GetWithStream(string fromFilePath, Tamir.SharpSsh.java.io.OutputStream stream)
    {
        cancelled = false;
        SftpChannel.get(fromFilePath, stream, m_monitor);
    }
    

    Next, create a wrapper for the Stream class that is compatible with Tamir.SharpSsh.java.io.OutputStream, such as the one below:

    using System.IO;
    using Tamir.SharpSsh.java.io;
    
    public class GenericSftpOutputStream : OutputStream
    {
        Stream stream;
        public GenericSftpOutputStream(Stream stream)
        {
            this.stream = stream;
        }
    
        public override void Write(byte[] buffer, int offset, int count)
        {
            stream.Write(buffer, offset, count);
        }
    
        public override void Flush()
        {
            stream.Flush();
        }
    
        public override void Close()
        {
            stream.Close();
        }
    
        public override bool CanSeek
        {
            get { return stream.CanSeek; }
        }
    
        public override long Seek(long offset, SeekOrigin origin)
        {
            return stream.Seek(offset, origin);
        }
    
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
    
            if (this.stream != null)
            {
                this.stream.Dispose();
                this.stream = null;
            }
        }
    }
    

    With those ingredients, you can now use OpenSSH to stream its data to the stream of your choice, as demonstrated below with a FileStream.

    using System.IO;
    using Tamir.SharpSsh;

    class Program
    {
        static void Main(string[] args)
        {
            var host = "hostname";
            var user = "username";
            var pass = "password";
            var file = "/some/remote/path.txt";
            var saveas = @"C:\some\local\path";
    
            var client = new Sftp(host, user, pass);
            client.Connect();
    
            using (var target = new GenericSftpOutputStream(File.Open(saveas, FileMode.OpenOrCreate)))
            {
                client.GetWithStream(file, target);
            }
    
            client.Close();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.