-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileNode.cs
More file actions
99 lines (84 loc) · 3.4 KB
/
FileNode.cs
File metadata and controls
99 lines (84 loc) · 3.4 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System.Text;
namespace Ramstack.FileProviders;
/// <summary>
/// Represents a file in the specified file provider.
/// </summary>
public sealed class FileNode : FileNodeBase
{
private readonly IFileInfo _file;
/// <inheritdoc />
public override bool Exists => _file.Exists;
/// <summary>
/// Gets the string representing the extension part of the file.
/// </summary>
public string Extension => FilePath.GetExtension(FullName);
/// <summary>
/// Gets the full path of the directory containing the file.
/// </summary>
public string DirectoryName => FilePath.GetDirectoryName(FullName);
/// <summary>
/// Gets the last modified time of the current file.
/// </summary>
public DateTimeOffset LastWriteTime => _file.LastModified;
/// <summary>
/// Gets a <see cref="DirectoryNode"/> instance representing the parent directory.
/// </summary>
public DirectoryNode Directory
{
get
{
var path = FilePath.GetDirectoryName(FullName);
var directory = Provider.GetDirectoryContents(path);
return new DirectoryNode(Provider, path, directory);
}
}
/// <summary>
/// Gets the length of the file in bytes, or <c>-1</c> for non-existing files.
/// </summary>
public long Length => _file.Length;
/// <summary>
/// Initializes a new instance of the <see cref="FileNode"/> class.
/// </summary>
/// <param name="provider">The <see cref="IFileProvider"/> associated with this file.</param>
/// <param name="path">The full path of the file within the provider.</param>
/// <param name="file">The <see cref="IFileInfo"/> associated with this file.</param>
internal FileNode(IFileProvider provider, string path, IFileInfo file) : base(provider, path)
{
if (file.IsDirectory)
Error_FileExpected();
_file = file;
}
/// <summary>
/// Returns a read-only stream of the current file.
/// </summary>
/// <returns>
/// A read-only stream.
/// </returns>
public Stream OpenRead() =>
_file.CreateReadStream();
/// <summary>
/// Returns a <see cref="StreamReader" /> with the specified character encoding that reads from the current text file.
/// </summary>
/// <param name="encoding">The character encoding to use.</param>
/// <returns>
/// A new <see cref="StreamReader"/> with the specified character encoding.
/// </returns>
public StreamReader OpenText(Encoding? encoding = null) =>
new StreamReader(_file.CreateReadStream(), encoding, detectEncodingFromByteOrderMarks: true, bufferSize: -1, leaveOpen: false);
/// <summary>
/// Returns the <see cref="IFileInfo"/> for the current instance.
/// </summary>
/// <returns>
/// The <see cref="IFileInfo"/> instance.
/// </returns>
[Obsolete("This method is obsolete and will be removed in a future release. Use the ToFileInfo method instead.", error: false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public IFileInfo GetFileInfo() =>
_file;
/// <inheritdoc />
public override IFileInfo ToFileInfo() =>
_file;
[DoesNotReturn]
private static void Error_FileExpected() =>
throw new ArgumentException("The specified path refers to a directory, not a file.");
}