开发者

c# Writing to a file without using using{}

开发者 https://www.devze.com 2022-12-16 04:29 出处:网络
I\'m writing a little application whereby I want to write the results of the operations to a file. Basically what I want to do is open a stream to a file (I\'m thinking FileStream, but am open to sug

I'm writing a little application whereby I want to write the results of the operations to a file.

Basically what I want to do is open a stream to a file (I'm thinking FileStream, but am open to suggestions), write data to the file, then close it at a later date.

So I've got a class called ReportFile, with methods:

.Create( string path )

.WriteInfo( string a, string b, string c ) ; //Or something like thi开发者_运维知识库s...

//Then sometime in the future

.Close()

So the class using the ReportFile class will create an instance, call WriteInfo(..) multiple times until it is finished doing whatever it needs to do, then call Close() at some point in the future.

Now I know I need to implement a Dispose pattern on the ReportFile class to ensure that if anything goes screwey that the handle to the file gets appropriately dealt with.

However I haven't been able to find anything thus far on the interweb showing a good way of keeping the file open and then checking to see if it needs to be closed, most of the examples just open the file do the writing, then close it - all within a using{} construct.

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

Anyone know of a good link to reference or any other advice ?

(Ohh I should mention that I don't do C# full time, it's only a hobby thing, so if this is a dumb question, my apologies ;-)


Is there a particular reason that you have to keep the file open?

In this situation I would simply open the file each time using FileMode.Append (or pass append=true to StreamWriter ctor) and then close it again afterwards with a using. eg:

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

Taking this approach you can don't really need a Create() or Close() method. The append=true will create the file if it does not exist.


The ReportFile would just have a TextWriter instance variable - which you would dispose within your own Dispose() method.

Why do you want to have an explicit Close() method, btw? Your callers should be using a using statement anyway, so why would they want to explicitly call Close as well?


Justa, i think that you're overthinking this feature a bit. The reason that you're seeing examples with the using construct is that using{} with file write is quite fast and safe.

Chances are that you're not opening and closing the file several times a second so there's no need to keep it open all the time and thus risking leaving the app without closing the file (which is a PITA to fix after the fact.) Using the using construct makes certain that the resource, your file in this case, is released and closed properly.

Another piece of advice for programming: don't worry about efficiency at the outset. Get it working first the simplest way you can and improve speed/performance later only if it's necessary.


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

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


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

An idea might be to build the content in memory then save it when you're ready. using a StringBuilder for example.


In the case you simply won't use a using {} construct, you can use the IDisposable interface: http://msdn.microsoft.com/en-us/library/system.idisposable.dispose.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消