-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
TempFile.cs
66 lines (53 loc) · 2.01 KB
/
TempFile.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Runtime.CompilerServices;
using Xunit;
namespace System.IO
{
/// <summary>
/// Represents a temporary file. Creating an instance creates a file at the specified path,
/// and disposing the instance deletes the file.
/// </summary>
public sealed class TempFile : IDisposable
{
/// <summary>Gets the created file's path.</summary>
public string Path { get; }
public TempFile(string path, long length = 0) : this(path, length > -1 ? new byte[length] : null)
{
}
public TempFile(string path, byte[] data)
{
Path = path;
if (data != null)
{
File.WriteAllBytes(path, data);
}
}
~TempFile() => DeleteFile();
public static TempFile Create(byte[] bytes, [CallerMemberName] string memberName = null, [CallerLineNumber] int lineNumber = 0)
{
return new TempFile(GetFilePath(memberName, lineNumber), bytes);
}
public static TempFile Create(long length = -1, [CallerMemberName] string memberName = null, [CallerLineNumber] int lineNumber = 0)
{
return new TempFile(GetFilePath(memberName, lineNumber), length);
}
public void AssertExists() => Assert.True(File.Exists(Path));
public string ReadAllText() => File.ReadAllText(Path);
public void Dispose()
{
GC.SuppressFinalize(this);
DeleteFile();
}
private void DeleteFile()
{
try { File.Delete(Path); }
catch { /* Ignore exceptions on disposal paths */ }
}
private static string GetFilePath(string memberName, int lineNumber)
{
string file = $"{IO.Path.GetRandomFileName()}_{memberName}_{lineNumber}";
return IO.Path.Combine(IO.Path.GetTempPath(), file);
}
}
}