Tech Off Thread

9 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Put text in a stream in C#.

Back to Forum: Tech Off
  • User profile image
    Blkbam

    How can I put text in a stream in C#?  Not to sound pushy but nevermind why, I need to pass a stream to a function.  It seems the .Net framework will let me create a stream ie:

    Stream s = null;

    but it won't let me create and instance of it, ie.

    Stream s = new Stream();

    I know I can get it from other objects such as a HttpRespone or a Assembly.GetSomethingFromStream.  Any help would be appreciated.

  • User profile image
    AndyC

    The class you're looking for is StreamWriter

  • User profile image
    Blkbam

    Close but no.  StreamWriter/Reader both take either files or other streams.  I need to put the text into a stream without creating a file.

  • User profile image
    blowdart

    Blkbam wrote:
    Close but no.  StreamWriter/Reader both take either files or other streams.  I need to put the text into a stream without creating a file.


    You mean a MemoryStream?

  • User profile image
    Harlequin

    blowdart wrote:
    Blkbam wrote: Close but no.  StreamWriter/Reader both take either files or other streams.  I need to put the text into a stream without creating a file.
    You mean a MemoryStream?


    That would be a good one, you can put whatever you want into a MemoryStream Smiley

  • User profile image
    JPrime

    This is off the top of my head, but you should be able to do something like:

    string s = "hello world";
    MemoryStream m = new MemoryStream(System.Text.Encoding.Default.GetBytes(s));

     

  • User profile image
    Sven Groot

    Blkbam wrote:
    Stream s = new Stream();

    Stream is an abstract class so it can't be created. You need to create a specific type of stream like a FileStream or a MemoryStream.

  • User profile image
    Blkbam

    Thanks all.  I got that working.  It seems thought that if I pass a MemoryStream into a function expecting a Stream object, it doesn't write any of the bytes but it retains the size.  Thankfully its my function so I just changed it to accept a MemoryStream instead.

  • User profile image
    Code​Monkey666

    MemorySteam is one of my most used classes, very handy.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.