C # Writing to a file without using {}

I am writing a small application in which I want to write the results of operations to a file.

Basically, what I want to do is open the stream to a file (I think FileStream is open to suggestions), write the data to the file, and then close it later.

So, I have a class called ReportFile , with methods:

.Create( string path ) .WriteInfo( string a, string b, string c ) ; //Or something like this... //Then sometime in the future .Close() 

Thus, a class using the ReportFile class will instantiate, call WriteInfo(..) several times until it finishes doing what it needs to do, and then call Close() at some point in the future.

Now I know that I need to implement the Dispose template in the ReportFile class, to make sure that if something goes about this, process the file descriptor.

However, I have not yet been able to find anything on the website showing a good way to keep the file open, and then check if it needs to be closed, most examples just open the file, write it, then close it - everything is inside the using{} construct.

In the ReportFile class , I want to check if the FileStream instance is closed so that I can close it and free the resource.

Does anyone know a good link to link or any other tips?

(Oh, I have to mention that I do not do C # full time, this is just a hobby, so if this is a stupid question, my apologies ;-)

+4
source share
6 answers

Is there any special reason why you should keep the file open?

In this situation, I just opened the file every time using FileMode.Append (or pass append = true for StreamWriter ctor) and then close it again with. eg:

 void WriteInfo(string text) { // Second parameter to StreamWriter ctor is append = true using(StreamWriter sw = new StreamWriter(logFilePath, true)) { sw.WriteLine(text); } } 

With this approach, you really don't need the Create () or Close () method. Append = true will create the file if it does not exist.

+3
source

ReportFile will only have a TextWriter instance variable that you would place in your own Dispose() method.

Why do you want to have an explicit Close() method, btw? Your callers should use the using statement anyway, so why should they also explicitly call Close ?

+3
source

Justa, I think you think too much about this feature. The reason you see the examples with the construct used is because using {} with a file write is pretty fast and safe.

Most likely, you do not open and close the file several times per second, so there is no need to constantly open it and therefore risk leaving the application without closing the file (which PITA fixes after the fact.) Using the using construct, make sure that the resource the file in this case is freed and closed properly.

Another programming tip: don't worry about efficiency from the start. Get it, first of all, in the easiest way and improve speed / performance later only if necessary.

+1
source

public void Create (string path) {mStream = new FileStream (path); }

public void Dispose () {if (mStream! = null) mStream.Dispose (); }

+1
source

I would suggest using the "using" construct and keep the file open only when saving.

The idea might be to create content in memory and then store it when you are ready. using, for example, StringBuilder.

0
source

In the case when you simply will not use the {} construct, you can use the IDisposable interface: http://msdn.microsoft.com/en-us/library/system.idisposable.dispose.aspx p>

0
source

All Articles