public static Hosts Load(string path) { var hosts = new Hosts(); using (var file = File.OpenText(path)) using (var reader = new JsonTextReader(file)) { var json = JToken.ReadFrom(reader); foreach (var entry in json.Children <JProperty>()) { string host = entry.Name; if (entry.Value.Type != JTokenType.String) { throw new ArgumentException("Host does contain a valid value: " + host); } string ipStr = (string)entry.Value; IPAddress ip; if (!IPAddress.TryParse(ipStr, out ip)) { throw new ArgumentException("IP address could not be parsed: " + ipStr); } if (!hosts.Add(host, ip)) { throw new ArgumentException("Host is assigned more than once: " + host); } } } return(hosts); }
private void SaveHosts(Hosts hosts) { hosts.Clear(); foreach (DataRow row in dt.Rows) { hosts.Add(row.Field <string>(0), IPAddress.Parse(row.Field <string>(1))); } Modified = false; Text = originalTitle; }