Click here to Skip to main content
13,457,843 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


23 bookmarked
Posted 6 Sep 2010

A Small C# File Creator Class

, 12 Sep 2010
Rate this:
Please Sign up or sign in to vote.
Introducing a small class that helps you in creating a temporary file with string/binary data and clean-up the file automatically


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


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:

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:

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!


  • 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


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


About the Author

Uwe Keim
Chief Technology Officer Zeta Producer Desktop CMS
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.

In his free time, he does climbing, running and mountain biking. Recently he became a father of a cute boy.

Some cool, free software from us:

German Developer Community  
Free Test Management Software - Intuitive, competitive, Test Plans. Download now!  
Homepage erstellen - Intuitive, very easy to use. Download now!  
Send large Files online for free by Email

You may also be interested in...


Comments and Discussions

GeneralMy vote of 4 Pin
kishore doni28-Aug-12 22:05
memberkishore doni28-Aug-12 22:05 
GeneralSmall but perfectly formed, and for that I give it Pin
Sacha Barber6-Sep-10 23:35
mvpSacha Barber6-Sep-10 23:35 
GeneralRe: Small but perfectly formed, and for that I give it Pin
Uwe Keim7-Sep-10 7:04
mvpUwe Keim7-Sep-10 7:04 
GeneralMy vote of 2 Pin
muellerh6-Sep-10 20:45
membermuellerh6-Sep-10 20:45 
GeneralRe: My vote of 2 Pin
Uwe Keim7-Sep-10 7:06
mvpUwe Keim7-Sep-10 7:06 
GeneralMy vote of 5 Pin
Eric Xue (brokensnow)6-Sep-10 10:09
memberEric Xue (brokensnow)6-Sep-10 10:09 
GeneralRe: My vote of 5 Pin
Uwe Keim6-Sep-10 19:07
mvpUwe 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03-2016 | 2.8.180323.1 | Last Updated 12 Sep 2010
Article Copyright 2010 by Uwe Keim
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid