Removing/Replacing Line feed from a text file in SFTP memory stream

0 votes
asked Jul 3, 2018 by Ritesh (350 points)

Hi There

I am writing a bespoke application to download the files from a SFTP server. What I want is to remove/replace a line feed from a text file in memory stream using Rebex SFTP component before downloading/writing the file.

Hope someone can help me this.

Thanks
Jai

Applies to: Rebex SFTP

1 Answer

0 votes
answered Jul 3, 2018 by Lukas Matyska (51,690 points)
edited Jul 3, 2018 by Lukas Pokorny

This question is somewhat beyond the scope of Rebex support because it deals with .NET's MemoryStream class rather than Rebex SFTP.

Please use general-purpose software development forums such as Stack Overflow for these kinds of questions.

However, this code do the trick:

// download file into memory
var downloaded = new MemoryStream();
client.GetFile("/file.txt", downloaded);

// prepare for reading
downloaded.Position = 0;

// read all as string
string text;
using (var reader = new StreamReader(downloaded))
{
    text = reader.ReadToEnd();
}

// replace <CR><LF> to single <LF>
string output = text.Replace("\r\n", "\n");
// or remove any <CR> and any <LF>
output = text.Replace("\r", "").Replace("\n", "");

// write output to file
File.WriteAllText(@"c:\data\output.txt", output);
// or to MemoryStream
var data = new MemoryStream(Encoding.UTF8.GetBytes(output));
...