Click here to Skip to main content
13,097,884 members (55,141 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


19 bookmarked
Posted 5 Jul 2010

A Small C# File Cloner Class

, 6 Sep 2010
Rate this:
Please Sign up or sign in to vote.
Introducing a small class that helps you in creating automatic copies of files and clean them up automatically, too


This article shows you a small class that you can use in various projects to simplify the following process:

  1. Create a temporary copy of a file.
  2. Perform operations on/with the temporary file.
  3. Delete the temporary copy of the file.


I created the class for a web application project where I read some Microsoft Excel documents into memory, manipulated the Excel documents in memory and finally sent them to the user's web browser.

Usually this task is as easy as doing some File operations and get a Stream object to the file. This works great when the file is not opened in Microsoft Excel. When it is opened, it is exclusively locked by Excel, resulting in exceptions in my code when I try to access the file stream.

What still works is to copy the file.

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

Using the Code

Using the code is really simple. Instead of writing something like:

// ...Do some operations to the file...
myFunctionThatOperatesOnTheFile( @"C:\Some\Path\File.xlsx" );

You write a code like:

using ( var tfc = new ZetaTemporaryFileCloner( @"C:\Some\Path\File.xlsx" ) )
    // ...Do some operations to the file...
    myFunctionThatOperatesOnTheFile( tfc.FilePath );

Easy, isn't it?

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 love to get!), please use the comments section below at the bottom of this article. Thank you!



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

Questionbetter.. Pin
mk.developer17-Jun-12 1:21
membermk.developer17-Jun-12 1:21 
GeneralI like this one too, and also gave it a 5 Pin
Sacha Barber6-Sep-10 23:37
mvpSacha Barber6-Sep-10 23:37 
GeneralRe: I like this one too, and also gave it a 5 Pin
Uwe Keim7-Sep-10 7:04
mvpUwe Keim7-Sep-10 7:04 
GeneralMy vote of 4 Pin
Eric Xue (brokensnow)6-Sep-10 10:06
memberEric Xue (brokensnow)6-Sep-10 10:06 
GeneralRe: My vote of 4 Pin
Uwe Keim6-Sep-10 19:06
mvpUwe Keim6-Sep-10 19:06 
GeneralNice approach Pin
Sushant Joshi6-Sep-10 6:47
memberSushant Joshi6-Sep-10 6:47 
GeneralRe: Nice approach Pin
Uwe Keim6-Sep-10 6:48
mvpUwe Keim6-Sep-10 6:48 
GeneralGood article Pin
Jeff.Ou19-Jul-10 23:46
memberJeff.Ou19-Jul-10 23:46 
GeneralRe: Good article Pin
Uwe Keim19-Jul-10 23:53
mvpUwe Keim19-Jul-10 23:53 
GeneralVote of 4 Pin
Charles Cox5-Jul-10 17:29
memberCharles Cox5-Jul-10 17:29 
GeneralRe: Vote of 4 Pin
Uwe Keim5-Jul-10 18:30
mvpUwe Keim5-Jul-10 18:30 

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
Web01 | 2.8.170813.1 | Last Updated 6 Sep 2010
Article Copyright 2010 by Uwe Keim
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid