Can I use a BinaryWriter without it closing the underlying stream?
I want to save information to a MemoryStream using a BinaryWriter but I want
to dispose of the BinaryWriter and keep the MemoryStream in case I wish to
reload information from the MemoryStream using BinaryReader.
Thanks
Pete 7 5219
Peter,
No, you can't, but you can get the contents of the MemoryStream before
the BinaryWriter is finished by calling ToArray on the MemoryStream. You
can then pass this to a new MemoryStream when you want to use it in a
BinaryReader again.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Peter Morris" <su*****@NOdroo pySPAMeyes.comw rote in message
news:%2******** *******@TK2MSFT NGP06.phx.gbl.. .
Can I use a BinaryWriter without it closing the underlying stream?
I want to save information to a MemoryStream using a BinaryWriter but I
want to dispose of the BinaryWriter and keep the MemoryStream in case I
wish to reload information from the MemoryStream using BinaryReader.
Thanks
Pete
Hi,
did you try it?
IIRC Close close the underlined stream. but as you are using a MemoryStream,
well "closing" it has not too much sense.
I would give it a try and post back if you have any problem.
--
Ignacio Machin http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"Peter Morris" <su*****@NOdroo pySPAMeyes.comw rote in message
news:%2******** *******@TK2MSFT NGP06.phx.gbl.. .
Can I use a BinaryWriter without it closing the underlying stream?
I want to save information to a MemoryStream using a BinaryWriter but I
want to dispose of the BinaryWriter and keep the MemoryStream in case I
wish to reload information from the MemoryStream using BinaryReader.
Thanks
Pete
>Can I use a BinaryWriter without it closing the underlying stream?
I want to save information to a MemoryStream using a BinaryWriter but I want to dispose of the BinaryWriter and keep the MemoryStream in case I wish to reload information from the MemoryStream using BinaryReader.
What I've done in the past is to create a proxy Stream class that does
nothing in its Close method, and just forwards all other method calls
to the stream it wraps. Then use it something like
new BinaryWriter(ne w NonClosingStrea m(yourActualStr eam))
Mattias
--
Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jon provides such a stream in his "misc utils" library: http://www.yoda.arachsys.com/csharp/miscutil/
It might not appear on the write-up, but I'm pretty sure it is
there... NonClosingStrea m or some-such; essentially acts as a
"decorator" for a stream and ignores Close() and Dispose() [it might
call Flush() instead, I can't recall]
Marc
My problem is that I want a MyClass.SaveToS tream
but as the stream is passed to me I have no (right) to close it. This is
because I have no knowledge of what the application wants to do with the
stream. For example, the app might save 4 objects to the same stream but
the first object will close the stream. It's a bit silly really isn't it
:-) Anyway, I just changed it to
SaveToStream(Bi naryWriter writer);
Pete This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Mark Miller |
last post by:
I have a char array and when I write it to a file using BinaryWriter the
position of the pointer is the size of the array + 1. For example: writing
char leaves the pointer at position 26 after starting at position 0. I
thought that char was 2 bytes, but this makes it seem as though it is just 1
when I write to a file. Why is this? I imagine the extra bit is just a null
bit (correct me if I'm wrong). I don't know if this helps but when I...
|
by: ThunderMusic |
last post by:
Hi,
In my app, I open a file using a FileStream then pass it to a
BinaryWriter. I then use the BinaryWriter instance to write to my file. But
a problem arose : The file never gets bigger than 1kb. The code calls the
bw.write(TheValue), but nothing is written after 1kb. I'm I missing
something?
Here's the way I create my FileStream and BinaryWriter :
Filename = System.Environment.CurrentDirectory() & "\msec.dat"
|
by: Filip Strugar |
last post by:
Considering that the BinaryWriter/BinaryReader object closes the underlaying
stream upon being gc collected, is the following code correct, and if it is
what is the reason preventing BinaryWriter object garbage collection after
the WriteSomething method is executed?
----
using System;
using System.IO;
|
by: Claire |
last post by:
I'm writing from a serial port into a MemoryStream via a BinaryWriter in my
protocol object.
After I've filled the memorystream, which is approx 200KB in size, I then
pass it up to my datahandler object where I plan to create a BinaryReader to
extract the data and format it ready for a database insert.
At the moment I close the binarywriter earlier than my call to the
datahandler object, and Ive just found out that closing the binarywriter...
|
by: Barguast |
last post by:
Is it necessary to call the close method for every BinaryWriter that I
create? Or is it just a way to close the underlying stream? For example is it
OK to do the following:
private void WriteUnicodeStringToStream (Stream s, string string)
{
BinaryWriter bw = new BinaryWriter (s, Encoding.Unicode);
bw.Write (string);
}
| |
by: Boris |
last post by:
// Example
BinaryWriter bw = new BinaryWriter(new
FileStream("c:\Test.bin",FileMode.Append));
BinaryWriter.Write writes data to the stream. However, it doesn't
automatically flush data any buffered data to the underlying device. What
determines *when* data is written to the underlying device, without calling
Flush function.
Thank you,
|
by: keithv |
last post by:
Hi,
The msdn doc for MemoryStream has two conflicting statements
about accessing a MemoryStream's buffer after it's been closed:
The buffer is still available on a MemoryStream once
the stream has been closed.
But later it says:
|
by: Lisa |
last post by:
Using C# .NET, I want to create a BinaryWriter to read Int32 data from a
file, but I don't see any facility to set the file pointer to a specific
point within the file.
Thanks for any help.
Lisa
|
by: Eugene |
last post by:
I'm trying to write a class which uses BinaryWriter as its base but allows
for queuing of write requests
Public Class QueuedBinaryWriter
Inherits BinaryWriter
I override all the Write methods like so:
Public Overloads Overrides Sub Write(ByVal Value As Byte)
m_Queue.Enqueue(New WriteRequest(MyBase.BaseStream.Position, Value))
End Sub
'same for all other Write variants
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |