StreamWriter Close method

Posted on 2008-06-10
Last Modified: 2013-12-17
I have a class that has streamwriter object declared on class level. sw is created in the class constructorI have methods that write to a file using sw. Something like:

internal class WriteData
    StreamWriter sw;
    // rest of the code

internal static class GlobalClass
    internal static WriteData writeData;

This way GlobalClass is accessible throghout the application. The problem is next:

1) How can I close streamwriter object, I need it to be closed when writeData is going to be destroyed? Calling sw.Close() in writeData destructor doesn't work.
2) is there any way I can check weather streamwriter object is "open", that is, weather something has been written to a file, other than the usual class level boolean variable that will be set to true when the method for writing data has been called?

If this looks confusing, I will explain better what you didn't understand.

Question by:Priest04
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
LVL 18

Accepted Solution

jcoehoorn earned 500 total points
ID: 21753893
> "This way GlobalClass is accessible throghout (sic) the application."

Actually, because of the 'internal' modifier it's only available in that assembly.  If you move the code to a class library project the rest of the application won't be able to use it.

1) Your WriteData class should implement the IDisposable pattern (google it).  Call sw.Close() in your Dispose() method.  sw.Close should close the stream, though I have heard of cases where windows still thinks the file is locked later- I think it's a bug in MS' implementation of the class.  

2) I'm assuming you open the stream in the constructor?  If that's the case, don't worry about it, though you might try checking that sw.BaseStream.CanWrite returns True.

LVL 18

Author Comment

ID: 21754358
>> Actually, because of the 'internal' modifier it's only available in that assembly.  If you move the code to a class library project the rest of the application won't be able to use it.

That was the intention.

Do you know why is sw already closed when object's desctructor is called, but can be called in Dispose method?
LVL 18

Expert Comment

ID: 21754384
Because .Net doesn't do 'destructors'.  When implemented correctly, you can call an IDisposable's Dispose() method as many times as you want without causing an error.
LVL 12

Expert Comment

ID: 21757098
you can open stream inside using block so once you came out of using block it will automatically disposed since it implements IDispose

and inside using block use try catch .. finally and in finally block close stream

so code will be like

using (create streamwriter here sw)
try {
catch (Exception ex)
LVL 18

Expert Comment

ID: 21759518
No need for the finally block or even to call sw.Close() if it was created with a using statement.  The using statement will make sure it's disposed.

But since the streamwriter is declared as a member of a class, he probably doesn't want a using statement here.

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity application has warrnings about VB6 calls. 2 48
What are MicroServices? 4 69
C# LINQ query question 11 33
AD Computer Objects in VB .Net 2 21
In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question