Created
July 17, 2014 15:44
-
-
Save reidev275/7d466cef3508e28a8a8d to your computer and use it in GitHub Desktop.
TemporaryDirectory - safe, self cleaning sandbox for file manipulation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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