47 lines
1.1 KiB
C#
47 lines
1.1 KiB
C#
using System;
|
|
using System.IO;
|
|
using ln.type;
|
|
using Microsoft.Win32.SafeHandles;
|
|
|
|
namespace ln.templates.service
|
|
{
|
|
public class TempFileStream : FileStream, IDisposable
|
|
{
|
|
public new void Dispose()
|
|
{
|
|
try
|
|
{
|
|
base.Dispose();
|
|
}
|
|
finally
|
|
{
|
|
if (File.Exists(Name))
|
|
File.Delete(Name);
|
|
}
|
|
}
|
|
|
|
public TempFileStream()
|
|
:base($"{GetUniqueFileName(Path.GetTempPath())}", FileMode.Create)
|
|
{
|
|
|
|
}
|
|
|
|
public TempFileStream(string extension)
|
|
:base($"{GetUniqueFileName(Path.GetTempPath(), extension)}", FileMode.Create)
|
|
{
|
|
}
|
|
|
|
|
|
public static string GetUniqueFileName(string path) => GetUniqueFileName(path, "");
|
|
public static string GetUniqueFileName(string path, string extension)
|
|
{
|
|
return String.Format("{1}/tmp.{2}{0}",
|
|
extension,
|
|
path,
|
|
BitConverter.GetBytes(DateTime.Now.ToUnixTimeMilliseconds()).ToHexString()
|
|
);
|
|
}
|
|
|
|
}
|
|
|
|
} |