C#: Temporary file handler for unit tests and such

Wrote a class that worked with files and needed to unit test its methods. Created a simple TemporaryFile class to easily create and dispose of the files I used in those tests. Here it is:

using System;
using System.IO;

namespace Geekality.IO
{
    public sealed class TemporaryFile : IDisposable
    {
        private readonly FileInfo file;
        public FileInfo FileInfo { get { return file; } }

        public TemporaryFile() : this(Path.GetTempFileName()) { }
        public TemporaryFile(string fileName) : this(new FileInfo(fileName)) { }
        public TemporaryFile(FileInfo temporaryFile)
        {
            file = temporaryFile;
        }
        public TemporaryFile(Stream initialFileContents) : this()
        {
            using (var file = new FileStream(this, FileMode.Open))
                initialFileContents.CopyTo(file);
        }

        public static implicit operator FileInfo(TemporaryFile temporaryFile)
        {
            return temporaryFile.file;
        }
        public static implicit operator string(TemporaryFile temporaryFile)
        {
            return temporaryFile.file.FullName;
        }
        public static explicit operator TemporaryFile(FileInfo temporaryFile)
        {
            return new TemporaryFile(temporaryFile);
        }

        private volatile bool disposed;
        public void Dispose()
        {
            try
            {
                file.Delete();
                disposed = true;
            }
            catch (Exception) { } // Ignore
        }
        ~TemporaryFile()
        {
            if (!disposed) Dispose();
        }
    }
}

This class will create a temporary file and delete it on dispose (or when the finalizer is called). It also supports implicit casting to the FileInfo object it contains and to its full path string. This makes it easy to use with methods taking those types of parameters. For example:

using(var file = new TemporaryFile())
{
    Assume.That(file.FileInfo.Length, Is.EqualTo(0))

    File.WriteAllText(file, "some text");

    file.FileInfo.Refresh();
    Assert.That(file.FileInfo.Length, Is.GreaterThan(0));
}

For fun I also created a Nuget package for this, so that it’s easy to add to projects when I need it. Works pretty perfectly, if I may say so myself :) Feedback is welcome!

Source: bitbucket.org/svish/geekality.io
Nuget: nuget.org/packages/Geekality.IO

  • kiquenet kiquenet

    It’s great, thanks.

  • Omer Raviv

    This is awesome, but I really need a TemporaryFolder class as well!

    • http://www.geekality.net/ Torleif Berger

      Well, using this pattern that shouldn’t be very difficult to add really. Let me know if you do and I can see if I can add it to the package :)