Skip to content

Instantly share code, notes, and snippets.

@reidev275
Created July 17, 2014 15:44
Show Gist options
  • Save reidev275/7d466cef3508e28a8a8d to your computer and use it in GitHub Desktop.
Save reidev275/7d466cef3508e28a8a8d to your computer and use it in GitHub Desktop.
TemporaryDirectory - safe, self cleaning sandbox for file manipulation
public class TemporaryDirectory : IDisposable
{
readonly string _directory;
public TemporaryDirectory()
{
_directory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(_directory);
}
public string Location { get { return _directory; } }
public void Dispose()
{
Directory.Delete(_directory, true);
}
}
using (var directory = new TemporaryDirectory())
{
var coverFilePath = Path.Combine(directory.Location, "cover.pdf");
File.WriteAllBytes(coverFilePath, bytes);
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment