Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

StreamWriter Close method

Posted on 2008-06-10
5
Medium Priority
?
580 Views
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.

Thanks,
Goran
0
Comment
Question by:Priest04
  • 3
5 Comments
 
LVL 18

Accepted Solution

by:
Joel Coehoorn earned 2000 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.

0
 
LVL 18

Author Comment

by:Priest04
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?
0
 
LVL 18

Expert Comment

by:Joel Coehoorn
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.
0
 
LVL 12

Expert Comment

by:renjurdevan
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)
{
...
}
finally
{
sw.close();
}
}
0
 
LVL 18

Expert Comment

by:Joel Coehoorn
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.
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

926 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