Tag Archives: Class

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

Java: Readable password generator

Needed something to generate random passwords and found Java Password Generator. It generates passwords which are gibberish, but still sort of readable, which I think is a lot nicer than pure gibberish.

I just wanted a simple ‘get me a new random password method’ though, and not all the applet and ‘make several passwords’ stuff. Decided to extract the useful parts and clean it up a bit. In case I need it again, or others need it, and since he states “share your source with others freely”, here is the result 🙂

Continue reading Java: Readable password generator