473,395 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

MemoryStream to String question

Does anybody have some sample code of converting a MemoryStream into a
String? I am having trouble doing this simple thing because I am tired and
under pressure. A code snippet would surly help. Thank you.
Nov 17 '05 #1
3 76854
Just Me,

You can just pass the memory stream to the constructor of the
StreamReader class, and then call the ReadToEnd method, like so:

public function GetStringFromStream(Stream stream)
{
// Create a stream reader.
using (StreamReader reader = new StreamReader(stream))
{
// Just read to the end.
return reader.ReadToEnd();
}
}

If you need to set the encoding for the content in the stream, you can
pass an Encoding instance to the StreamReader constructor.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Just Me" <Just Me@Microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Does anybody have some sample code of converting a MemoryStream into a
String? I am having trouble doing this simple thing because I am tired
and under pressure. A code snippet would surly help. Thank you.

Nov 17 '05 #2

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:OC****************@TK2MSFTNGP10.phx.gbl...
Just Me,

public function GetStringFromStream(Stream stream)


function? You forgot

using function = System.String; // I'm evil

Or are you stuck in Delphi- or JavaScript-mode, Nicholas?
Amagad, don't tell us you've been fooling around with C again. You Naught
Naughty Boy! =)

Happy coding
- Michael S
Nov 17 '05 #3
DelphiApp.cpp
#include "stdafx.h"
#define begin {
#define end }
#define WriteLn Console::WriteLine
#define program int
#using <mscorlib.dll>
using namespace System;

program _tmain()
begin
WriteLn("Hello World");
end
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Tmenke | last post by:
Is there a way to superscript a single character in a string for example to show a number raised to a exponent ? Eg lblResult.Caption = " 52 in4 " Where the 4 would be superscripted ? ...
6
by: Kian Goh | last post by:
Hello, I would like to replace all the occurrence of "(" and ")" in C++ std::string to "\(" and "\)". For example: string s = "(abc)|(toto)|(lala)" will be become "\(abc\)|\(toto\)|\(lala\)"
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
7
by: David Schwartz | last post by:
I have a string A, and I want to write a substring (of varying lengths) starting at the beginning of A, to another string. Can someone show me how to do that?
5
by: John Baro | last post by:
I have a richtextbox which I want the "literal" rtf of. richtextbox.rtf returns {\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\uc1 }\r\n\0 when i put this into a string I get...
7
by: Ioannis Vranos | last post by:
I had reported this as a bug: Description: Default indexed property of System::String crashes for object with stack semantics. int main()
5
by: news.chi.sbcglobal.net | last post by:
I have a question about string literals with C++/CLI. Is there a difference between the following two lines? String ^s1 = gcnew String("Hello"); String ^s2 = gcnew String(L"Hello"); I have...
2
by: pauldepstein | last post by:
string word = 'a' ; is illegal code. However, string word; word = 'a'; is legal. What is the difference? The 2nd one doesn't even generate a warning on my bloodshed dev...
1
by: kevinridge | last post by:
Hello, I have confusing string question. I have a string like this (example): <?php $a = " {FILE: hello} hello contents {FILE: hello2} hello2 contents "; ?>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.