mirror of https://github.com/dahall/Vanara.git
29 lines
847 B
C#
29 lines
847 B
C#
using System;
|
|
using System.IO;
|
|
using static Vanara.PInvoke.Kernel32;
|
|
|
|
namespace Vanara.PInvoke.Tests
|
|
{
|
|
public class TempFile : IDisposable
|
|
{
|
|
public const string tmpstr = @"Temporary";
|
|
|
|
public TempFile(Kernel32.FileAccess dwDesiredAccess, FileShare dwShareMode, FileMode dwCreationDisposition = FileMode.OpenOrCreate, FileFlagsAndAttributes dwFlagsAndAttributes = FileFlagsAndAttributes.FILE_ATTRIBUTE_NORMAL) : this()
|
|
{
|
|
hFile = CreateFile(FullName, dwDesiredAccess, dwShareMode, null, dwCreationDisposition, dwFlagsAndAttributes, IntPtr.Zero);
|
|
}
|
|
|
|
public TempFile(string contents = tmpstr)
|
|
{
|
|
FullName = Path.GetTempFileName(); File.WriteAllText(FullName, contents);
|
|
}
|
|
|
|
public string FullName { get; }
|
|
public SafeHFILE hFile { get; }
|
|
|
|
void IDisposable.Dispose()
|
|
{
|
|
hFile?.Dispose(); File.Delete(FullName);
|
|
}
|
|
}
|
|
} |