public HostsFile() { var path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.System) + "\\drivers\\etc\\hosts"; var contents = Common.Globals.ReadFile(path); using (var reader = new StringReader(contents)) { string line; while ((line = reader.ReadLine()) != null) { if (line.Trim().StartsWith("#")) { CommentLines.Add(line); } else { var m = Regex.Match(line, @"\s*(\d+\.\d+\.\d+\.\d+)\s+([^\s]+)(\s+(#.*))?"); if (m.Success) { Entries[m.Groups[2].Value.ToLower()] = new HostEntry() { IpAddress = m.Groups[1].Value, HostName = m.Groups[2].Value.ToLower(), Comment = m.Groups[4].Success ? m.Groups[4].Value : "" }; } } } } }
/// <summary> /// Adds a comment line to the this comment block. /// </summary> /// <param name="line">The line to add.</param> public void AddCommentLine(ICommentLine line) { Location = CommentLines.Count == 0 ? line.Location : Location.Create(line.Location.SourceTree, new TextSpan(Location.SourceSpan.Start, line.Location.SourceSpan.End - Location.SourceSpan.Start)); CommentLines.Add(line); }
private void SaveCommentIfNonEmpty(string line) { string commentContent = line.StripSqlCommentMarkers(); if (!string.IsNullOrEmpty(commentContent)) { CommentLines.Add(commentContent); } }