Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6468093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T05:51:28+00:00 2026-05-25T05:51:28+00:00

I am trying write a stream to the ram instead of a file. I

  • 0

I am trying write a stream to the ram instead of a file. I tried doing this:

Stream stream = new MemoryStream();
BinaryFormatter bFormatter = new BinaryFormatter();
bFormatter.Serialize(stream, objectToSerialize);
stream.Close();
return stream;

But when I look at the stream after I have supposedly written to it it is saying “Length = ‘stream.Length’ threw an exception of type ‘System.ObjectDisposedException'”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T05:51:28+00:00Added an answer on May 25, 2026 at 5:51 am

    You’re calling stream.Close(), which is exactly the same as calling Dispose() on the stream.

    Just remove that line of code, and you should be fine. Basically, you need to leave the MemoryStream open when it’s returned.

    On a different note, depending on what you’re going to do, you may also want to reset the stream’s position. I suspect you’ll want:

    Stream stream = new MemoryStream();
    BinaryFormatter bFormatter = new BinaryFormatter();
    bFormatter.Serialize(stream, objectToSerialize);
    stream.Position = 0;
    return stream;
    

    This works the same as your code, but does not Dispose() the stream (since it’s no longer calling stream.Close()), and also resets it to the start position, which is often required if you want to read the object/data back out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to Write an object stream into a XAML file but i
I´m trying write strings in diffrent languages to a rtf file. I hav tried
So, I was trying to write a function like this: void append_to_stream(std::ostream &stream) {
Trying to write a couple of functions that will encrypt or decrypt a file
Im trying to write a single byte at a certain location in a file.
I'm trying to write my own logging class and use it as a stream:
I'm writing custom ActionFilterAttribute and trying to write some data directly into output stream
I'm trying to write data to a simple txt file in PHP and open
I'm trying to write a simple server that will grab an mp3 file from
I'm trying to write out to the response stream - but it is failing,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.