class Program
{
class EnumLocalCollection
{
private List<LocalCollectionInfo> _infos;
public EnumLocalCollection()
{
_infos = new List<LocalCollectionInfo>();
}
private string InvokeCmd()
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine("ipconfig");
p.StandardInput.WriteLine("exit");
return p.StandardOutput.ReadToEnd();
}
public bool Parse()
{
string input = InvokeCmd();
var convert = new Func<MatchCollection, int, List<string>>(Converts);
List<string> names = convert.Invoke(Regex.Matches(input, @"(?:PPP|Ethernet) adapter (.*):"), 1);
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);
if (names.Count != ips.Count)
return false;
for (int i = 0; i < names.Count; i++)
{
_infos.Add(new LocalCollectionInfo() { Name = names[i], IP = ips[i] });
}
return true;
}
public ReadOnlyCollection<LocalCollectionInfo> LocalCollectionInfos
{
get { return this._infos.AsReadOnly(); }
}
private List<string> Converts(MatchCollection mcs, int group)
{
List<string> rts = new List<string>();
for (int i = 0; i < mcs.Count; i++)
rts.Add(mcs[i].Value);
return rts;
}
}
class LocalCollectionInfo
{
public string Name { get; set; }
public string IP { get; set; }
public override string ToString()
{
return string.Format("{0}-{1}", Name, IP);
}
}

static void Main()
{
//调用
EnumLocalCollection lc = new EnumLocalCollection();
if (!lc.Parse())
{
Console.WriteLine("error");
return;
}
ReadOnlyCollection<LocalCollectionInfo> infos = lc.LocalCollectionInfos;
for (int i = 0; i < infos.Count; i++)
{
Console.WriteLine(infos[i].ToString());
}
Console.ReadKey();
}
}


机器人,这首歌学会了没有?

我们的目标是->没有蛀牙!