Click to See Complete Forum and Search --> : System.IO.Stream ?


Sachinkalse
September 22nd, 2004, 04:10 AM
How to convert string or StringBuilder to System.IO.Stream using C# ?
pls guide

darwen
September 22nd, 2004, 06:05 AM
What are you trying to do exactly ?

You can WRITE and READ from streams, not 'convert'.

e.g.


string sHelloThere = "Hello there";

System.IO.MemoryStream stream = new System.IO.MemoryStream();
System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream);
writer.Write(sHelloThere);
writer.Close();


Similarly to read from a stream you can do :


byte [] abData = // from wherever

System.IO.MemoryStream stream = new System.IO.MemoryStream(abData);
System.IO.BinaryReader reader = new System.IO.BinaryReader(stream);
string sRead = reader.ReadString();
reader.Close();


Darwen.

Sachinkalse
September 23rd, 2004, 07:40 PM
Hello
Thanks for reply.
Well, I am trying to load a file in Rich text box using

RichTextBox.LoadFile Method (Stream, RichTextBoxStreamType)

but my data is in string format (I am generating an RTF file at runtime), thats why i want to convert it in to stream

please guide

regards