Easy to Use Temporary File and Directories

I have to create a lot of temporary files in a project and I was sick of the usual way of doing things.  It was too verbose and prone to errors.  I created temporary disposable file and directory objects that are very easy to use. The files are created in Isolated Storage if no parent path is provided. I’ve never used Isolated Storage but it seems like a good place to create temporary files since it can be kept separate from other applications and users. You don’t have to clutter up the usual temp directory or worry about write permissions to your application’s working directory.

using (var directory = new TemporaryDirectory(optionalPath))
{
    //use directory.DirectoryPath to do stuff
}
using (var file = new TemporaryFile(optionalDirectory, optionalExtension)
{
    //use file.FilePath to do stuff
}
    public class TemporaryFile : IDisposable
    {
        public TemporaryFile() :
            this(null, ".temp")
        {
        }

        protected TemporaryFile(string directory, string extension)
        {
            var filename = String.Format("{0}.{1}", Guid.NewGuid(), extension);

            if (String.IsNullOrEmpty(directory))
            {
                var store = IsolatedStorageFile.GetUserStoreForAssembly();
                using(var stream = store.CreateFile(filename))
                {
                    FilePath =
                        stream.GetType().GetField("m_FullPath", BindingFlags.Instance | BindingFlags.NonPublic).GetValue
                            (stream).ToString();
                }
            }
            else
            {
                FilePath = Path.Combine(directory, filename);
                using (File.Create(FilePath))
                {
                }
            }
        }

        public string FilePath { get; private set; }

        #region IDisposable Members

        public void Dispose()
        {
            Delete();
            GC.SuppressFinalize(this);
        }

        #endregion

        ~TemporaryFile()
        {
            Delete();
        }

        private void Create(string path)
        {
            FilePath = path;
            using (File.Create(FilePath))
            {
            }
        }

        private void Delete()
        {
            if (FilePath == null) return;
            File.Delete(FilePath);
            FilePath = null;
        }
    }
    public class TemporaryDirectory : IDisposable
    {
        public TemporaryDirectory(string path)
        {
            var directoryName = Guid.NewGuid().ToString();

            if(String.IsNullOrEmpty(path))
            {
                var store = IsolatedStorageFile.GetUserStoreForAssembly();
                store.CreateDirectory(directoryName);
                var storePath =
                    store.GetType().GetField("m_RootDir", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(
                        store).
                        ToString();
                DirectoryPath = Path.Combine(storePath, directoryName);
            }
            else
            {
                DirectoryPath = Path.Combine(path, directoryName);
                Directory.CreateDirectory(DirectoryPath);
            }
        }

        public TemporaryDirectory() : this (null)
        {

        }

        public string DirectoryPath { get; private set; }

        #region IDisposable Members

        public void Dispose()
        {
            Delete();
            GC.SuppressFinalize(this);
        }

        #endregion

        ~TemporaryDirectory()
        {
            Delete();
        }

        private void Delete()
        {
            if (DirectoryPath == null) return;
            Directory.Delete(DirectoryPath, true);
            DirectoryPath = null;
        }
    }
Tagged with: , ,
Posted in .Net

Leave a comment