public static void AddHost(string ip, string name, string desc,bool use,bool addLine) { List<HostItem> hosts = HostsDal.GetHosts(""); HostItem item = new HostItem(ip,name,desc,use,addLine); hosts.Add(item); HostsDal.SaveHosts("", hosts); }
public static List<HostItem> GetHosts(string path) { path = GetFileName(path); if (File.Exists(path)) { using (StreamReader reader = new StreamReader(path, Encode)) { List<HostItem> list = new List<HostItem>(); while (!reader.EndOfStream) { string str = reader.ReadLine().Trim(); if ((str != "# 102.54.94.97 rhino.acme.com # source server") && (str != "# 38.25.63.10 x.acme.com # x client host")) { if (string.IsNullOrEmpty(str)) { //MessageBox.Show(str); if (list.Count > 0) { list[list.Count - 1].AddLine = true; } } else { Match match = RegItem.Match(str); if (match.Success) { string ip = match.Result("$1"); string name = match.Result("$2"); string desc = match.Result("$3"); bool use = !str.StartsWith("#"); HostItem item = new HostItem(ip, name, desc, use,false); list.Add(item); } } } } return list; } } return null; }
private List<HostItem> GetGridHost() { List<HostItem> list = new List<HostItem>(); foreach (DataGridViewRow row in (IEnumerable) this.dataGridView1.Rows) { string str = Convert.ToString(row.Cells[0].Value).Trim(); string name = Convert.ToString(row.Cells[1].Value).Trim(); string str2 = Convert.ToString(row.Cells[2].Value).Trim(); bool use = Convert.ToBoolean(row.Cells[3].Value); bool addLine = Convert.ToBoolean(row.Cells[4].Value); if ((!string.IsNullOrEmpty(str) || !string.IsNullOrEmpty(name)) || !string.IsNullOrEmpty(str2)) { if (use && (list.Find(delegate (HostItem i) { return i.Name.Equals(name, StringComparison.OrdinalIgnoreCase) && i.IsUsing; }) != null)) { MessageBox.Show("域名:" + name + "存在重复配置,请检查后再重新保存"); return null; } HostItem item = new HostItem(str, name, str2, use, addLine); list.Add(item); } } return list; }