Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C#

A Small C# File Creator Class

Rate me:
Please Sign up or sign in to vote.
4.83/5 (10 votes)
12 Sep 2010CPOL2 min read 40.7K   668   23   7
Introducing a small class that helps you in creating a temporary file with string/binary data and clean-up the file automatically

Introduction

In this article, I'll introduce a small class which you can use to write strings or binary data to a temporary file and have the file automatically deleted again after using it. So in short, you can do the following steps with the class:

  1. Create a temporary file on disk with the content of a string (or binary object) in memory
  2. Perform operations on/with the temporary file
  3. Delete the temporary file from disk

This article is a follow-up to my article "A small C# File Cloner class".

Background

I came up with the class as I was using some third party library that required a file with data as a parameter, but I had the data in memory only (in a string class).

Instead of manually creating the file, writing the file to disk, passing it to the function, and then manually cleaning up the file again (and be aware of possible exceptions), I wanted to have a dead simple solution. I hope this one is such a solution.

So I created a class that implements the IDisposable interface, creates a temporary file with the in-memory data in its constructor, and deletes the temporary file again in its destructor or via the IDisposable.Dispose method.

Using the Code

Writing to Files

Using the code should be really simple. Just create an instance of the ZetaTemporaryFileCreator class inside a using directive, passing the data to write to the file in the constructor:

C#
using ( var tfc = new ZetaTemporaryFileCreator( "Some text to write to the file." ) )
{
    // ...Do some operations on the file...
    myFunctionThatOperatesOnAFileRatherThanOnInMemoryData( tfc.FilePath );
}

The using block ensures that the temporary file is being deleted again, even in case of an exception occurring.

Reading from Files

You can also use the code for functions that write to a file and you want to read the results back into memory later.

Use the parameterless constructor together with one (or both) of the properties FileContentString and FileContentBinary:

C#
using ( var tfc = new ZetaTemporaryFileCreator() )
{
    // ...Call a function that generates data to a file...
    myFunctionThatWritesDataToAFileRatherThanIntoMemory( tfc.FilePath );
 
    // ...Further process the data from the file in memory...
    myFunctionThatOperatesOnInMemoryData( tfc.FileContentString );
}

Again, the using block ensures that the temporary file is being deleted again, even in case of an exception occurring.

Points of Interest

In this article, I've shown you a really small class which allows for automatic creation and removal of a temporary file. The download contains the complete C# class which you can simply drop into your own projects.

For your questions, comments and feedback (which I really love to get!), please use the comments section below at the bottom of this article. Thank you!

History

  • 2010-09-12 - Added parameterless constructor and the properties FileContentString and FileContentBinary to read back from file into memory
  • 2010-09-06 - First release to CodeProject.com

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Zeta Software GmbH
Germany Germany
Uwe does programming since 1989 with experiences in Assembler, C++, MFC and lots of web- and database stuff and now uses ASP.NET and C# extensively, too. He has also teached programming to students at the local university.

➡️ Give me a tip 🙂

In his free time, he does climbing, running and mountain biking. In 2012 he became a father of a cute boy and in 2014 of an awesome girl.

Some cool, free software from us:

Windows 10 Ereignisanzeige  
German Developer Community  
Free Test Management Software - Intuitive, competitive, Test Plans.  
Homepage erstellen - Intuitive, very easy to use.  
Offline-Homepage-Baukasten

Comments and Discussions

 
GeneralMy vote of 4 Pin
D-Kishore28-Aug-12 22:05
D-Kishore28-Aug-12 22:05 
GeneralSmall but perfectly formed, and for that I give it Pin
Sacha Barber6-Sep-10 23:35
Sacha Barber6-Sep-10 23:35 
GeneralRe: Small but perfectly formed, and for that I give it Pin
Uwe Keim7-Sep-10 7:04
sitebuilderUwe Keim7-Sep-10 7:04 
GeneralMy vote of 2 Pin
nobodyxxxxx6-Sep-10 20:45
nobodyxxxxx6-Sep-10 20:45 
GeneralRe: My vote of 2 Pin
Uwe Keim7-Sep-10 7:06
sitebuilderUwe Keim7-Sep-10 7:06 
Hehe, no doubt, you must be from Germany.
My personal 24/7 webcam  
Free Test Management Software - Intuitive, competitive, Test Plans. Download now!  
Free Website Builder - Intuitive, very easy to use. Download now!

GeneralMy vote of 5 Pin
Eric Xue (brokensnow)6-Sep-10 10:09
Eric Xue (brokensnow)6-Sep-10 10:09 
GeneralRe: My vote of 5 Pin
Uwe Keim6-Sep-10 19:07
sitebuilderUwe Keim6-Sep-10 19:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.