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;
|
2020-03-26 23:02:15 +00:00
|
|
|
|
using AlphaPath = Alphaleonis.Win32.Filesystem.Path;
|
2019-12-14 20:11:39 +00:00
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Common
|
|
|
|
|
{
|
|
|
|
|
public class TempFile : IDisposable
|
|
|
|
|
{
|
|
|
|
|
public FileInfo File { get; private set; }
|
2020-03-26 23:02:15 +00:00
|
|
|
|
public AbsolutePath Path => (AbsolutePath)File.FullName;
|
2019-12-14 20:11:39 +00:00
|
|
|
|
public bool DeleteAfter = true;
|
|
|
|
|
|
|
|
|
|
public TempFile(bool deleteAfter = true, bool createFolder = true)
|
2020-04-03 03:57:59 +00:00
|
|
|
|
: this(new FileInfo((string)GetTempFilePath()))
|
2019-12-14 20:11:39 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-03 03:57:59 +00:00
|
|
|
|
private static AbsolutePath GetTempFilePath()
|
|
|
|
|
{
|
2020-04-03 20:19:10 +00:00
|
|
|
|
var path = (@"temp\" + Guid.NewGuid()).RelativeTo(AbsolutePath.EntryPoint).WithExtension(Consts.TempExtension);
|
2020-04-03 03:57:59 +00:00
|
|
|
|
path.Parent.CreateDirectory();
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-14 20:11:39 +00:00
|
|
|
|
public TempFile(FileInfo file, bool deleteAfter = true, bool createFolder = true)
|
|
|
|
|
{
|
|
|
|
|
this.File = file;
|
|
|
|
|
if (createFolder && !file.Directory.Exists)
|
|
|
|
|
{
|
|
|
|
|
file.Directory.Create();
|
|
|
|
|
}
|
|
|
|
|
this.DeleteAfter = deleteAfter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (DeleteAfter)
|
|
|
|
|
{
|
|
|
|
|
this.File.Delete();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|