public static void DeleteHostByIP(string ip, string fname) { IPRecord check = new IPRecord(ip, "", ""); String fileData = ""; IPRecord[] current = GetRecords(fname); foreach (IPRecord a in current) { if (a.HostMathes(check)) { //Don't put record } else { //Console.WriteLine("I was here at else.\n"); fileData = fileData + a.GetHostLine(); } } //Restore comments/IPV6 records to the host file if (!string.IsNullOrEmpty(hostfilecomments)) { fileData = hostfilecomments + fileData; } File.WriteAllText(fname, fileData); }
public bool HostMathes(IPRecord a) { if (hostname.ToLower().Equals(a.hostname.ToLower())) { return(true); } if (IP.Equals(a.IP)) { return(true); } return(false); }
private static void SwapRecords(ref IPRecord a, ref IPRecord b) { IPRecord temp = new IPRecord(a.IP, a.hostname, a.alias); a.hostname = b.hostname; a.IP = b.IP; a.alias = b.alias; b.hostname = temp.hostname; b.IP = temp.IP; b.alias = temp.alias; }
public static void AddRecords(IPRecord r, string fname)//="c:/Windows/System32/drivers/etc/hosts") { bool existingRecord = false; String fileData = ""; IPRecord[] current = GetRecords(fname); foreach (IPRecord a in current) { if (!a.HostMathes(r)) { //Console.WriteLine("I was here at If.\n"); fileData = fileData + a.comments + "\r\n"; fileData = fileData + a.GetHostLine(); } else { //Console.WriteLine("I was here at else.\n"); if (string.IsNullOrEmpty(r.comments)) { fileData = fileData + r.comments + "\r\n"; } else { fileData = fileData + a.comments + "\r\n"; } fileData = fileData + r.GetHostLine(); existingRecord = true; } } if (!existingRecord) { fileData = fileData + r.comments + "\r\n"; fileData = fileData + r.GetHostLine(); } //Restore comments/IPV6 records to the host file if (!string.IsNullOrEmpty(hostfilecomments)) { fileData = hostfilecomments + fileData; } File.WriteAllText(fname, fileData); }