|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace ndimg
|
|
|
|
|
{
|
|
|
|
|
sealed class TempDirectory : IDisposable
|
|
|
|
|
{
|
|
|
|
|
public DirectoryInfo Directory { get; }
|
|
|
|
|
public TempDirectory(DirectoryInfo location)
|
|
|
|
|
{
|
|
|
|
|
Directory = new DirectoryInfo(Path.Combine(location.FullName, TempFile.GenerateLocation()));
|
|
|
|
|
Directory.Create();
|
|
|
|
|
}
|
|
|
|
|
public TempDirectory() : this(new DirectoryInfo(TempFile.TempFileLocation)) { }
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Directory.Delete(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sealed class TempFile : IDisposable, IAsyncDisposable
|
|
|
|
|
{
|
|
|
|
|
public static string GenerateLocation()
|
|
|
|
|
{
|
|
|
|
|
var time = DateTime.Now;
|
|
|
|
|
return $"ndimg-{time.Year}-{time.Month}-{time.Day}-{time.Hour}.{time.Minute}.{time.Second}.{time.Millisecond}-{Guid.NewGuid().ToString()}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists)
|
|
|
|
|
{
|
|
|
|
|
if (Stream != null)
|
|
|
|
|
Stream.Dispose();
|
|
|
|
|
File.Delete();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async ValueTask DisposeAsync()
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists)
|
|
|
|
|
{
|
|
|
|
|
if(Stream!=null)
|
|
|
|
|
await Stream.DisposeAsync();
|
|
|
|
|
File.Delete();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~TempFile()
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists)
|
|
|
|
|
{
|
|
|
|
|
try { File.Delete(); } catch { }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string TempFileLocation { get; set; }
|
|
|
|
|
static TempFile()
|
|
|
|
|
{
|
|
|
|
|
TempFileLocation = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), GenerateLocation())).FullName;
|
|
|
|
|
|
|
|
|
|
AppDomain.CurrentDomain.DomainUnload += (o, e) =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
foreach (var file in new DirectoryInfo(TempFileLocation).GetFiles()) try
|
|
|
|
|
{
|
|
|
|
|
file.Delete();
|
|
|
|
|
}
|
|
|
|
|
catch { }
|
|
|
|
|
Directory.Delete(TempFileLocation, true);
|
|
|
|
|
}
|
|
|
|
|
catch { }
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
public FileInfo File { get; }
|
|
|
|
|
public Stream Stream { get; }
|
|
|
|
|
public TempFile(DirectoryInfo location)
|
|
|
|
|
{
|
|
|
|
|
var nam = Path.Combine(location.FullName, GenerateLocation() + ".tmp");
|
|
|
|
|
File = new FileInfo(nam);
|
|
|
|
|
Stream = new FileStream(nam, FileMode.Create);
|
|
|
|
|
}
|
|
|
|
|
public TempFile() : this(new DirectoryInfo(TempFileLocation)) { }
|
|
|
|
|
|
|
|
|
|
private TempFile(FileInfo fi)
|
|
|
|
|
{
|
|
|
|
|
File = fi;
|
|
|
|
|
Stream = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static TempFile WithNoStream()
|
|
|
|
|
{
|
|
|
|
|
return new TempFile(new FileInfo(Path.Combine(new DirectoryInfo(TempFileLocation).FullName, GenerateLocation() + ".tmp")));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|