using System;
using System.IO;
namespace Elsa.Testing.Shared.Helpers
{
public class TemporaryFolder : IDisposable
{
private readonly bool _deleteOnDispose;
public TemporaryFolder(bool deleteOnDispose = true)
{
Folder = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
_deleteOnDispose = deleteOnDispose;
}
public string Folder { get; }
///
/// Gets a path string for a file or folder which is contained within the .
///
/// The relative path, within the temporary folder.
/// A path for content within the temporary folder.
public string GetContainedPath(string relativePath)
{
Directory.CreateDirectory(Folder);
return Path.Combine(Folder, relativePath);
}
public void Dispose()
{
if (_deleteOnDispose)
if (Directory.Exists(Folder))
Directory.Delete(Folder, true);
}
public override string ToString() => Folder;
}
}