1: class EnumLocalCollection
2: { 3: private List<LocalCollectionInfo> _infos;
4:
5: public EnumLocalCollection()
6: { 7: _infos = new List<LocalCollectionInfo>();
8: }
9:
10: private string InvokeCmd()
11: { 12: Process p = new Process();
13: p.StartInfo.FileName = "cmd.exe";
14: p.StartInfo.UseShellExecute = false;
15: p.StartInfo.RedirectStandardInput = true;
16: p.StartInfo.RedirectStandardOutput = true;
17: p.StartInfo.RedirectStandardError = true;
18: p.StartInfo.CreateNoWindow = true;
19: p.Start();
20:
21: p.StandardInput.WriteLine("ipconfig"); 22: p.StandardInput.WriteLine("exit"); 23:
24: return p.StandardOutput.ReadToEnd();
25: }
26:
27: public bool Parse()
28: { 29: string input = InvokeCmd();
30:
31: var convert = new Func<MatchCollection, int, List<string>>(Converts);
32: List<string> names = convert.Invoke(Regex.Matches(input, @"(?:PPP|Ethernet) adapter (.*):"), 1);
33: List<string> ips = convert.Invoke(Regex.Matches(input, @"IPv??4?? Address.*:\s(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"), 1); 34:
35: if (names.Count != ips.Count)
36: return false;
37:
38: for (int i = 0; i < names.Count; i++)
39: { 40: _infos.Add(new LocalCollectionInfo() { Name = names[i], IP = ips[i] }); 41: }
42:
43: return true;
44: }
45:
46: public ReadOnlyCollection<LocalCollectionInfo> LocalCollectionInfos
47: { 48: get { return this._infos.AsReadOnly(); } 49: }
50:
51: private List<string> Converts(MatchCollection mcs, int group)
52: { 53: List<string> rts = new List<string>();
54: for (int i = 0; i < mcs.Count; i++)
55: rts.Add(mcs[i].Groups[group].Value);
56: return rts;
57: }
58: }
59:
60: class LocalCollectionInfo
61: { 62: public string Name { get; set; } 63: public string IP { get; set; } 64:
65: public override string ToString()
66: { 67: return string.Format("{0}-{1}", Name, IP); 68: }
69: }