473,499 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MemoryStream instance length is 0 after stream has been written

I'm trying to write new stream from string and i can't figure out why my
memory stream
instance is null after i have writen to it with stream writer.

Here is an example.

MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter();

writer.Write ("my string");

at this point i check what does my stream contain and it says .Length = 0
so why is that ? How can it be 0 if i've just written to it with writer ?
Nov 29 '06 #1
2 4869
I'm trying to write new stream from string and i can't figure out why
my
memory stream
instance is null after i have writen to it with stream writer.
Here is an example.

MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter();
writer.Write ("my string");

at this point i check what does my stream contain and it says .Length
= 0 so why is that ? How can it be 0 if i've just written to it with
writer ?
Call writer.Flush();

Best Regards,
Dustin Campbell
Developer Express Inc.
Nov 29 '06 #2

Mikus Sleiners wrote:
I'm trying to write new stream from string and i can't figure out why my
memory stream
instance is null after i have writen to it with stream writer.

Here is an example.

MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter();

writer.Write ("my string");

at this point i check what does my stream contain and it says .Length = 0
so why is that ? How can it be 0 if i've just written to it with writer ?
How does your StreamWriter know to write to your stream? You have not
specified the stream that the stream writer should write to. Also, you
should use the using pattern if possible:

MemoryStream stream = new MemoryStream();
using (StreamWriter writer = new StreamWriter(stream))
{
writer.Write("my string");
}

Notice that the memory stream was passed into the constructor of the
stream writer.

Chris

Nov 29 '06 #3

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

Similar topics

7
9514
by: Stingray | last post by:
Are there any know problems with using a MemoryStream as a backing store for a CryptoStream? I'm trying to simply encrypt and decrypt text in memory. I'd like to create some simple methods to...
1
11113
by: xmlguy | last post by:
PREVIOUS BACKGROUND POST: I am trying to reuse a Memory Stream for loading and transforming the xml it contains Basically I have defined following interfaces: Class Render {
7
16385
by: Donovan | last post by:
I can't believe this is causing me as much difficulty as it is, but I have an Infragistics UltraTreeview control that I want to persist whatever the user has in the tree. It has a method SaveAsXml...
2
5153
by: Brian Mitchell | last post by:
Hello, I am using a MemoryStream to serialize a class but when I use the GetBuffer method it returns exactly 1024 bytes even though the stream is only 675 bytes. If I look at the...
13
2829
by: Don | last post by:
When I run the following code, the MemoryStream's Position is always set to 762 instead of 0, which is what I would expect: Dim bmp As Image Dim ms As MemoryStream bmp = New...
5
26214
by: Naamat | last post by:
Hello, I am the sample FPSEPublish (http://blog.baeke.info/blog/_archives/2005/3/3/393158.html) code to upload a document to Sharepoint (WSS). This works perfectly for samll documents. ...
10
17805
by: Asaf | last post by:
Hi, I am trying to Compress & Decompress a DataSet. When running this code I am receiving the error: System.Xml.XmlException was unhandled Message="Root element is missing."...
7
4183
by: Maciej Oszutowski | last post by:
Hi, I'm going to port my PE manipulation library (written in C) to managed c++ class library. I would like to have opportunity to read content not only from files, but also from memory, for...
1
5745
by: MSwanston | last post by:
Hi I need some help with saving retreiving data from the cache, and how best to structure my code. FYI am working in VS2005/under .NET2 Framework. Ok, we have a series of reports that get run via...
0
7180
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
7395
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
5485
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,...
1
4921
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3108
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
311
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.