wabbajack/Wabbajack.Common/Util/TempFolder.cs
2019-12-14 14:11:39 -06:00

46 lines
1.2 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Wabbajack.Common
{
public class TempFolder : IDisposable
{
public DirectoryInfo Dir { get; private set; }
public bool DeleteAfter = true;
public TempFolder(bool deleteAfter = true)
{
this.Dir = new DirectoryInfo(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()));
this.Dir.Create();
this.DeleteAfter = deleteAfter;
}
public TempFolder(DirectoryInfo dir, bool deleteAfter = true)
{
this.Dir = dir;
if (!dir.Exists)
{
this.Dir.Create();
}
this.DeleteAfter = deleteAfter;
}
public TempFolder(string addedFolderPath, bool deleteAfter = true)
: this(new DirectoryInfo(Path.Combine(Path.GetTempPath(), addedFolderPath)), deleteAfter: deleteAfter)
{
}
public void Dispose()
{
if (DeleteAfter)
{
Utils.DeleteDirectory(this.Dir.FullName);
}
}
}
}