You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.6 KiB
88 lines
2.6 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ndview
|
|
{
|
|
public 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);
|
|
}
|
|
}
|
|
public sealed class TempFile : IDisposable, IAsyncDisposable
|
|
{
|
|
public static string GenerateLocation()
|
|
{
|
|
var time = DateTime.Now;
|
|
return $"{time.Year}-{time.Month}-{time.Day}-{time.Hour}.{time.Minute}.{time.Second}.{time.Millisecond}-{Guid.NewGuid().ToString()}";
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (File.Exists)
|
|
{
|
|
Stream.Dispose();
|
|
File.Delete();
|
|
}
|
|
}
|
|
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
if (File.Exists)
|
|
{
|
|
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 { }
|
|
};
|
|
}
|
|
private 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)) { }
|
|
}
|
|
}
|