C#: Temporary file handler for unit tests and such

Published:

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!