注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

天边云E家 shaoruisky

IT博文共享,知识的海洋

 
 
 

日志

 
 

查看网络配置  

2012-05-11 10:11:36|  分类: C# |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
public void GetNetAdapter()
        {
          
            try
            {
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection nics = mc.GetInstances();
                foreach (ManagementObject nic in nics)
                {
                    try
                    {
                        if (Convert.ToBoolean(nic["IPEnabled"]) == true)
                        {
                            comboBox1.Items.Add(nic["Caption"].ToString());
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "IP地址更改工具", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

 //如果有网卡,默认选中第一块网卡
            if (comboBox1.Items.Count > 0)
            {
                comboBox1.SelectedIndex = 0;
                GetNetInfor(comboBox1.SelectedItem.ToString());
            }
         }

public void GetNetInfor(string strNetAdapterName)
        {           
            //获取选中的网卡的IP,网关,子网掩码,DNS,备用DNS信息
            string strIP, strSubnet, strGateway, strDNS, strDNSBY;
            strIP = "0.0.0.0";
            strSubnet = "0.0.0.0";
            strGateway = "0.0.0.0";
            strDNS = "0.0.0.0";
            strDNSBY = "0.0.0.0";
            try
            {
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection nics = mc.GetInstances();
                foreach (ManagementObject nic in nics)
                {
                    try
                    {
                        if (Convert.ToBoolean(nic["IPEnabled"]) == true)
                        {
                            if (nic["Caption"].ToString() == strNetAdapterName)
                            {
                                if ((nic["IPAddress"] as String[]).Length > 0 )
                                {
                                    strIP = (nic["IPAddress"] as String[])[0];
                                }
                                if ((nic["IPSubnet"] as String[]).Length > 0 )
                                {
                                    strSubnet = (nic["IPSubnet"] as String[])[0];
                                }
                                if ((nic["DefaultIPGateway"] as String[]).Length > 0 )
                                {
                                    strGateway = (nic["DefaultIPGateway"] as String[])[0];
                                }
                                if ((nic["DNSServerSearchOrder"] as String[]).Length > 0 )
                                {
                                    strDNS = (nic["DNSServerSearchOrder"] as String[])[0];
                                    strDNSBY = (nic["DNSServerSearchOrder"] as String[])[1];
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            catch (Exception)
            {
            }
            textBox1.Text = strIP;
            textBox2.Text = strSubnet;
            textBox3.Text = strGateway;
            textBox4.Text = strDNS;
            textBox5.Text = strDNSBY;
        }

  评论这张
 
阅读(174)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016