-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathHostsFile.cs
More file actions
44 lines (41 loc) · 1.14 KB
/
HostsFile.cs
File metadata and controls
44 lines (41 loc) · 1.14 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
using BytecodeApi.Extensions;
using BytecodeApi.IO;
using System.Collections.ObjectModel;
namespace BytecodeApi.Win32.SystemInfo;
/// <summary>
/// Provides a snapshot of the hosts file in %SYSTEMROOT%\drivers\etc\hosts.
/// </summary>
public sealed class HostsFile
{
/// <summary>
/// Gets all hosts file entries.
/// </summary>
public ReadOnlyCollection<HostsFileEntry> Entries { get; }
private HostsFile(IEnumerable<HostsFileEntry> entries)
{
Entries = entries.ToReadOnlyCollection();
}
/// <summary>
/// Creates a new <see cref="HostsFile" /> instance and loads all entries from the hosts file.
/// </summary>
/// <returns>
/// The <see cref="HostsFile" /> this method creates.
/// </returns>
public static HostsFile Load()
{
return new
(
File
.ReadAllLines(KnownPaths.HostsFile)
.Select(line => line.Trim().Replace('\t', ' '))
.Where(line => !line.StartsWith('#'))
.Where(line => line.Contains(' '))
.Select(line => new HostsFileEntry
{
IPAddress = line.SubstringUntil(' ').Trim(),
HostName = line.SubstringFrom(' ').Trim()
})
.ToArray()
);
}
}