2019-12-14 20:11:39 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Common
|
|
|
|
|
{
|
2020-03-28 13:33:39 +00:00
|
|
|
|
public class TempFolder : IAsyncDisposable
|
2019-12-14 20:11:39 +00:00
|
|
|
|
{
|
2020-03-23 12:57:18 +00:00
|
|
|
|
public AbsolutePath Dir { get; }
|
2019-12-14 20:11:39 +00:00
|
|
|
|
public bool DeleteAfter = true;
|
|
|
|
|
|
|
|
|
|
public TempFolder(bool deleteAfter = true)
|
|
|
|
|
{
|
2020-03-23 12:57:18 +00:00
|
|
|
|
Dir = new AbsolutePath(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()));
|
|
|
|
|
if (!Dir.Exists)
|
|
|
|
|
Dir.CreateDirectory();
|
|
|
|
|
DeleteAfter = deleteAfter;
|
2019-12-14 20:11:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-23 12:57:18 +00:00
|
|
|
|
public TempFolder(AbsolutePath dir, bool deleteAfter = true)
|
2019-12-14 20:11:39 +00:00
|
|
|
|
{
|
2020-03-23 12:57:18 +00:00
|
|
|
|
Dir = dir;
|
2019-12-14 20:11:39 +00:00
|
|
|
|
if (!dir.Exists)
|
|
|
|
|
{
|
2020-03-23 12:57:18 +00:00
|
|
|
|
Dir.Create();
|
2019-12-14 20:11:39 +00:00
|
|
|
|
}
|
2020-03-23 12:57:18 +00:00
|
|
|
|
DeleteAfter = deleteAfter;
|
2019-12-14 20:11:39 +00:00
|
|
|
|
}
|
2020-03-28 13:33:39 +00:00
|
|
|
|
public async ValueTask DisposeAsync()
|
2019-12-14 20:11:39 +00:00
|
|
|
|
{
|
2020-03-23 12:57:18 +00:00
|
|
|
|
if (DeleteAfter && Dir.Exists)
|
2019-12-14 20:11:39 +00:00
|
|
|
|
{
|
2020-03-28 13:33:39 +00:00
|
|
|
|
await Utils.DeleteDirectory(Dir);
|
2019-12-14 20:11:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|