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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:34:04+00:00 2026-05-21T17:34:04+00:00

If I have the following code: FileStream fs = new FileStream(…); TextWriter tw =

  • 0

If I have the following code:

FileStream fs = new FileStream(...);
TextWriter tw = new StreamWriter(fs);

Am I supposed to call only tw.Close(), or fs.Close() as well? I’m using the TextWriter persistently throughout an application, so I can’t just wrap it in a Using(…) block, since I need to be able to use it at multiple points.

Otherwise, I would just write it as:

using (FileStream fs = new FileStream(...))
{
    using (TextWriter tw = new StreamWriter(fs))
    {
       // do stuff
    }
}

And avoid the issue at all together. But, I don’t really know what to do in this circumstance. Do I close tw, fs, or both?

Also, more generally: If I compose multiple streams together, such as C(B(A)), can I call Close / Dispose on C and then not worry about having to call it on B and A?

  • 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-21T17:34:04+00:00Added an answer on May 21, 2026 at 5:34 pm

    You only need to close the StreamWriter.

    From the documentation of the StreamWriter.Close method:

    Closes the current StreamWriter object
    and the underlying stream.

    Also notable in the documentation of the method:

    This implementation of Close calls the
    Dispose method passing a true value.

    This means that closing and disposing the StreamWriter are equivalent. You don’t have to dispose the object after closing it, or close it before disposing it. The FileStream.Close method does the same, so closing or disposing the StreamWriter will also dispose the stream.

    If you wouldn’t be able to find information like this for a class, you can always dispose both the writer and the stream to be safe. Disposing an object that has already been disposed does not cause any problem, and the method would just do nothing. Just make sure to close them in the right order, i.e. closing the writer before the stream.

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

Sidebar

Related Questions

I have this code FileStream fs = new FileStream(Scores.txt, FileMode.OpenOrCreate, FileAccess.Write); StreamWriter sw =
I have the following code: using (MemoryStream str = new MemoryStream()) { Program.api.GetDocument(result, str);
I have the following code: private void Process(string path) { using (FileStream fs =
I have the following code: try { FileStream FS = new FileStream(this.InFile, FileMode.Open, FileAccess.Read);
I have the following lines of code: xslt.Load(XmlReader.Create(new FileStream(@C:\website\TransList.xslt, System.IO.FileMode.Open))); xslt.Transform(mydoc.CreateReader(),null, sw); It works
I have following code: Dim fs As FileStream fs = New FileStream(path, FileMode.Create) Dim
I have he following code HttpPostedFileBase files = Request.Files[0]; Stream fileStream = files.InputStream; using
I have following code class User attr_accessor :name end u = User.new u.name =
I'm trying to send a file using ftp. I have the following code: string
I have lots of code like this: FileStream fs = File.Open(@C:\Temp\SNB-RSS.xml, FileMode.Open); using (XmlTextReader

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.