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 8138895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:38:56+00:00 2026-06-06T11:38:56+00:00

I have the following code, which uses a stream to open and modify an

  • 0

I have the following code, which uses a stream to open and modify an Open XML document, and then save the new binary representation of that stream:

MemoryStream stream = null;
try
{
    stream = new MemoryStream();
    stream.Write(this.GetBinaryRepresentation(), 0, this.GetBinaryRepresentation().Length);

    using (WordprocessingDocument document = WordprocessingDocument.Open(stream, true))
    {
        OfficeDocument.ModifyDocument(document);
        this.SetBinaryRepresentation(stream.ToArray());
        stream = null;
    }
}
finally
{
    if (stream != null)
    {
        stream.Dispose();
    }
}

I had originally used two using blocks (one for the MemoryStream and the second for the WordprocessingDocument), but received warning CA2202: “Object ‘stream’ can be disposed more than once in method…” Per the MSDN article, I modified the code to above (converting the outer using to a try), but I am still receiving this warning.

I’m unsure of how I can structure this method to ensure that Dispose is called exactly once on the stream. I would prefer not to simply suppress this warning since the MSDN article states that you shouldn’t rely on Dispose being safely callable multiple times.

  • 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-06-06T11:38:58+00:00Added an answer on June 6, 2026 at 11:38 am

    The reason that the example from the MSDN article did not work for you is that they set the stream to null as soon as they enter the using block, whereas you use the stream inside your using block and set the stream to null after. If an exception is thrown before your stream = null statement, stream would be disposed of as the using block is exited, and then again in your finally block.

    Unfortunately, since you need to access your stream after document has updated it, I don’t see a clean way to use their example of setting stream = null within your using statement to avoid the multiple Dispose() calls. An alternative would be to you could declare both stream and document outside of the try block, and then clean both of them up inside your finally, like so:

    MemoryStream stream = null;
    WordprocessingDocument document = null;
    try
    {
        stream = new MemoryStream();
        stream.Write(this.GetBinaryRepresentation(), 0, this.GetBinaryRepresentation().Length);
    
        document = WordprocessingDocument.Open(stream, true));
    
        OfficeDocument.ModifyDocument(document);
        this.SetBinaryRepresentation(stream.ToArray()); 
    }
    finally
    {
        if( document != null)
        {
            document.Dispose();
        }
        // Catch the case where an error occurred before document was defined.
        else
        {
            stream.Dispose();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I have the following code which uses knockout.js, var allJobberDetailsArray = []; getAllJobberDetailsArray();
I have the following code which re-uses a CookieContainer which logs in on the
I have the following code, in which Boost.Local uses a function callback to load
I have the following code, which uses a Unicode string class from a library
I have the following code which uses an array to write the result to
I have the following code which uses the System.Timers.Timer: // an instance variable Timer
I have the following code which uses nested for-each loops to traverse two ArrayList
I have the following C code which uses a stored procedure to insert 2
I have the following piece of code which uses ICU macros in order to
I have the following piece of code which uses the ALAssets library to store

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.