protected void ParseEntries()
{
// Parse hosts file lines
foreach (string line in OrigContents)
{
// Dont add empty lines or comments
string cLine = line.Trim();
if (String.IsNullOrEmpty(cLine) || cLine[0] == '#')
continue;
// Add line if we have a valid address and hostname
Match M = Regex.Match(
cLine,
@"^([\s|\t]+)?(?<address>[a-z0-9\.:]+)[\s|\t]+(?<hostname>[a-z0-9\.\-_\s]+)$",
RegexOptions.IgnoreCase
);
// Add line
if (M.Success)
{
string hostname = M.Groups["hostname"].Value.ToLower();
if (!Entries.ContainsKey(hostname))
{
IPAddress addy = null;
if (IPAddress.TryParse(M.Groups["address"].Value, out addy))
Entries.Add(hostname, addy);
}
}
}
// Make sure we have a localhost loopback! Save aswell, so its available for future
if (!Entries.ContainsKey("localhost"))
{
OrigContents.Add("127.0.0.1\tlocalhost");
Entries.Add("localhost", IPAddress.Loopback);
}
}