Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 69 additions & 77 deletions Source/NETworkManager.Models/HostsFileEditor/HostsFileEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,16 @@ private static IEnumerable<HostsFileEntry> GetHostsFileEntries()

if (result.Success)
{
Log.Debug("GetHostsFileEntries - Line matched: " + line);
Log.Debug($"GetHostsFileEntries - Line matched: {line}");

var entry = new HostsFileEntry
{
IsEnabled = !result.Groups[1].Value.Equals("#"),
IPAddress = result.Groups[2].Value,
Hostname = result.Groups[3].Value.Replace(@"\s", "").Trim(),
Comment = result.Groups[4].Value.TrimStart('#', ' '),
Line = line
};
(
!result.Groups[1].Value.Equals("#"),
result.Groups[2].Value,
result.Groups[3].Value.Replace(@"\s", "").Trim(),
result.Groups[4].Value.TrimStart('#', ' '),
line
);

// Skip example entries
if (!entry.IsEnabled)
Expand All @@ -157,7 +157,7 @@ private static IEnumerable<HostsFileEntry> GetHostsFileEntries()
}
else
{
Log.Debug("GetHostsFileEntries - Line not matched: " + line);
Log.Debug($"GetHostsFileEntries - Line not matched: {line}");
}
}

Expand Down Expand Up @@ -201,31 +201,34 @@ private static HostsFileEntryModifyResult EnableEntry(HostsFileEntry entry)
return HostsFileEntryModifyResult.ReadError;
}

bool entryFound = false;
var entryFound = false;

for (var i = 0; i < hostsFileLines.Count; i++)
{
if (hostsFileLines[i] == entry.Line)
{
entryFound = true;
if (hostsFileLines[i] != entry.Line)
continue;

hostsFileLines.RemoveAt(i);
hostsFileLines.Insert(i, CreateEntryLine(new HostsFileEntry
{
IsEnabled = true,
IPAddress = entry.IPAddress,
Hostname = entry.Hostname,
Comment = entry.Comment,
Line = entry.Line
}));

break;
}
entryFound = true;

Log.Debug($"EnableEntry - Found entry: {hostsFileLines[i]}");
hostsFileLines.RemoveAt(i);

var newEntry = new HostsFileEntry(
true,
entry.IPAddress,
entry.Hostname,
entry.Comment
);

Log.Debug($"EnableEntry - Enabling entry: {newEntry.Line}");
hostsFileLines.Insert(i, newEntry.Line);

break;
}

if (!entryFound)
{
Log.Warn($"EnableEntry - Entry not found in hosts file: {entry.Line}");
Log.Warn($"EnableEntry - Entry not found in hosts file: {entry}");

return HostsFileEntryModifyResult.NotFound;
}
Expand Down Expand Up @@ -281,26 +284,29 @@ private static HostsFileEntryModifyResult DisableEntry(HostsFileEntry entry)
return HostsFileEntryModifyResult.ReadError;
}

bool entryFound = false;
var entryFound = false;

for (var i = 0; i < hostsFileLines.Count; i++)
{
if (hostsFileLines[i] == entry.Line)
{
entryFound = true;
if (hostsFileLines[i] != entry.Line)
continue;

hostsFileLines.RemoveAt(i);
hostsFileLines.Insert(i, CreateEntryLine(new HostsFileEntry
{
IsEnabled = false,
IPAddress = entry.IPAddress,
Hostname = entry.Hostname,
Comment = entry.Comment,
Line = entry.Line
}));

break;
}
entryFound = true;

Log.Debug($"DisableEntry - Found entry: {hostsFileLines[i]}");
hostsFileLines.RemoveAt(i);

var newEntry = new HostsFileEntry(
false,
entry.IPAddress,
entry.Hostname,
entry.Comment
);

Log.Debug($"DisableEntry - Disabling entry: {newEntry.Line}");
hostsFileLines.Insert(i, newEntry.Line);

break;
}

if (!entryFound)
Expand Down Expand Up @@ -360,7 +366,8 @@ private static HostsFileEntryModifyResult AddEntry(HostsFileEntry entry)
return HostsFileEntryModifyResult.ReadError;
}

hostsFileLines.Add(CreateEntryLine(entry));
Log.Debug($"AddEntry - Adding entry: {entry.Line}");
hostsFileLines.Add(entry.Line);

try
{
Expand Down Expand Up @@ -415,19 +422,22 @@ private static HostsFileEntryModifyResult EditEntry(HostsFileEntry entry, HostsF
return HostsFileEntryModifyResult.ReadError;
}

bool entryFound = false;
var entryFound = false;

for (var i = 0; i < hostsFileLines.Count; i++)
{
if (hostsFileLines[i] == entry.Line)
{
entryFound = true;
if (hostsFileLines[i] != entry.Line)
continue;

hostsFileLines.RemoveAt(i);
hostsFileLines.Insert(i, CreateEntryLine(newEntry));
entryFound = true;

break;
}
Log.Debug($"EditEntry - Found entry: {hostsFileLines[i]}");
hostsFileLines.RemoveAt(i);

Log.Debug($"EditEntry - Editing entry: {newEntry.Line}");
hostsFileLines.Insert(i, newEntry.Line);

break;
}

if (!entryFound)
Expand Down Expand Up @@ -487,18 +497,19 @@ private static HostsFileEntryModifyResult DeleteEntry(HostsFileEntry entry)
return HostsFileEntryModifyResult.ReadError;
}

bool entryFound = false;
var entryFound = false;

for (var i = 0; i < hostsFileLines.Count; i++)
{
if (hostsFileLines[i] == entry.Line)
{
entryFound = true;
if (hostsFileLines[i] != entry.Line)
continue;

hostsFileLines.RemoveAt(i);
entryFound = true;

break;
}
Log.Debug($"DeleteEntry - Found entry: {hostsFileLines[i]}");
hostsFileLines.RemoveAt(i);

break;
}

if (!entryFound)
Expand All @@ -521,25 +532,6 @@ private static HostsFileEntryModifyResult DeleteEntry(HostsFileEntry entry)
return HostsFileEntryModifyResult.Success;
}

/// <summary>
/// Create a line for the hosts file entry.
/// </summary>
/// <param name="entry">Entry to create the line for.</param>
/// <returns>Line for the hosts file entry.</returns>
private static string CreateEntryLine(HostsFileEntry entry)
{
var line = entry.IsEnabled ? "" : "# ";

line += $"{entry.IPAddress} {entry.Hostname}";

if (!string.IsNullOrWhiteSpace(entry.Comment))
{
line += $" # {entry.Comment}";
}

return line.Trim();
}

/// <summary>
/// Create a "daily" backup of the hosts file (before making a change).
/// </summary>
Expand Down
24 changes: 23 additions & 1 deletion Source/NETworkManager.Models/HostsFileEditor/HostsFileEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ public HostsFileEntry(bool isEnabled, string ipAddress, string hostname, string
IPAddress = ipAddress;
Hostname = hostname;
Comment = comment;

var line = isEnabled ? "" : "# ";

line += $"{ipAddress} {hostname}";

if (!string.IsNullOrWhiteSpace(comment))
line += $" # {comment}";

Line = line;
}

/// <summary>
Expand All @@ -62,8 +71,21 @@ public HostsFileEntry(bool isEnabled, string ipAddress, string hostname, string
/// <param name="hostname">Host name(s) of the host.</param>
/// <param name="comment">Comment of the host.</param>
/// <param name="line">Line of the entry in the hosts file.</param>
public HostsFileEntry(bool isEnabled, string ipAddress, string hostname, string comment, string line) : this(isEnabled, ipAddress, hostname, comment)
public HostsFileEntry(bool isEnabled, string ipAddress, string hostname, string comment, string line)
{
IsEnabled = isEnabled;
IPAddress = ipAddress;
Hostname = hostname;
Comment = comment;
Line = line;
}

/// <summary>
/// Overrides the ToString method to return the line of the entry in the hosts file.
/// </summary>
/// <returns>Line of the entry in the hosts file.</returns>
public override string ToString()
{
return Line;
}
}
Loading