Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Writing to a file without using using{}

Tags:

c#

.net

file-io

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 this...

//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 ;-)

like image 732
JustAPleb Avatar asked Jan 07 '10 15:01

JustAPleb


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What do you mean by C?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


1 Answers

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.

like image 169
Ash Avatar answered Oct 08 '22 06:10

Ash