Move TempFile to own class
This commit is contained in:
@ -1,12 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Media;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.IO;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace WGneu.Helpers {
|
||||
@ -63,46 +61,5 @@ namespace WGneu.Helpers {
|
||||
UseShellExecute = true,
|
||||
});
|
||||
}
|
||||
|
||||
public sealed class TemporaryFile : IDisposable {
|
||||
private int Usages = 0;
|
||||
public string FilePath { get; private set; }
|
||||
|
||||
public TemporaryFile() : this(null) { }
|
||||
|
||||
public TemporaryFile(string? ext) : this(Path.Combine(Path.GetTempPath(), "Kelwin"), ext) { }
|
||||
|
||||
public TemporaryFile(string dir, string? ext) {
|
||||
FilePath = Path.Combine(dir, Path.GetRandomFileName().Replace(".", "") + (ext != null ? $".{ext}" : ""));
|
||||
Usages++;
|
||||
Create();
|
||||
}
|
||||
|
||||
~TemporaryFile() {
|
||||
Delete();
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
if (--Usages == 0) {
|
||||
Delete();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
|
||||
public TemporaryFile NewReference() {
|
||||
Usages++;
|
||||
return this;
|
||||
}
|
||||
|
||||
private void Create() {
|
||||
using (File.Create(FilePath)) { };
|
||||
}
|
||||
|
||||
private void Delete() {
|
||||
if (FilePath == null) return;
|
||||
File.Delete(FilePath);
|
||||
FilePath = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user