Skip to content

Instantly share code, notes, and snippets.

@poulfoged
Created September 8, 2015 08:05
Show Gist options
  • Save poulfoged/df026e8b12e19122a743 to your computer and use it in GitHub Desktop.
Save poulfoged/df026e8b12e19122a743 to your computer and use it in GitHub Desktop.
internal class TemporaryDirectory : IDisposable
{
private readonly DirectoryInfo _directoryInfo;
private bool _disposed;
public TemporaryDirectory()
{
_directoryInfo = new DirectoryInfo(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")));
_directoryInfo.Create();
}
public DirectoryInfo Info
{
get { return _directoryInfo; }
}
protected virtual void Dispose(bool disposing)
{
if (_disposed)
return;
try
{
_directoryInfo.Delete(true);
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
_disposed = true;
}
~TemporaryDirectory()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment