2008年7月10日

   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: }

用的时候这样用:

   1: EnumLocalCollection lc = new EnumLocalCollection();
   2: if (!lc.Parse())
   3: {
   4:     Console.WriteLine("error");
   5:     return;
   6: }
   7: ReadOnlyCollection<LocalCollectionInfo> infos = lc.LocalCollectionInfos;
   8: for (int i = 0; i < infos.Count; i++)
   9: {
  10:     Console.WriteLine(infos[i].ToString());
  11: }
posted @ 2008-07-10 18:15 micYng 阅读(245) | 评论 (0)编辑

导航

公告

垃圾Comment,Spam杀无赦!

<2008年7月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

统计

与我联系

搜索

 

常用链接

留言簿(8)

我参与的团队

我的标签

随笔分类

随笔档案

文章分类

相册

收藏夹

.Net Enterprise Library

.Net Remoting

ASP.NET

Blog Friends:)

C# category

C# forum&blogs

C# Toolkit

CodeSmith Usages

cPP related

Design Pattern

Opensource project

Pervious Blog

Useful tip

积分与排名

最新评论

阅读排行榜

评论排行榜