简体   繁体   中英

How can I convert StreamWriter to a Stream?

I am trying to create a file using FileHelpers and then write that file out using SshNet all in memory.

So far I have the following:

        var engine = new FileHelperEngine<MyObject>();
        MemoryStream ms = new MemoryStream();
        StreamWriter sw = new StreamWriter(ms);
        engine.WriteStream(sw, MyData);

        SshHelper ssh = new SshHelper("","","");
        ssh.WriteFile("MyPath", sw.BaseStream);

However my issue is with the WriteFile method since it requires a Stream parameter and when I run my code I am getting an empty file.

How can I convert my StreamWriter (sw variable) into a Stream parameter?

EDIT: I've tried both(not at the same time):

ms.Seek(0, SeekOrigin.Begin);
ms.Position = 0;

and it still writes a 0 byte file.

I further tested by using FileHelper to write to my local disk to verify that I have data. (Which I do)

your MemoryStream is the stream, the StreamWriter is just writing to it. Try passing ms in, instead of sw.

WriteFile之前尝试将您的流的位置设置为 0: ms.Seek( 0, SeekOrigin.Begin )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM