我用asp.net,通过WMI取得网卡信息,比如现在我的电脑上有四个网卡,其中一个插了网线,然后我可以得到他们四个网卡的网卡名称,而且我也可以修改四个网卡的IP,唯一的问题就是,其它三个没插网线的网卡的IP,子掩码,默认网关取不到,IP,和子掩码都显示0.0.0.0,默认网关是空的,可以得到DNS,我想知道是什么原因,还是说在没插网线的情况下用WMI编程是没法取到的,有什么办法可以解决吗,希望给个方法。我添的固定IP,也获取不到,只要没插网线

解决方案 »

  1.   

    System.Net.IPAddress[] ipAddressList = System.Net.Dns.GetHostAddresses(Environment.MachineName);
      

  2.   


    select * from Win32_NetworkAdapterConfiguration 
      

  3.   


     public class Utils
        {
            public Utils() { }        public Utils(string IP, string UserID, string PassWord)
            {
                this.IP = IP;
                this.PassWord = PassWord;
                this.UserID = UserID;
            }        public ManagementObjectCollection getServiceCollection(string stringQuery, Action<ManagementObject> action)
            {
                ManagementObjectSearcher query;
                ManagementObjectCollection queryCollection = null;
                ObjectQuery oq;            ConnectionOptions co = new ConnectionOptions();            string stringMachineName = IP;            if (stringMachineName.Trim().Length == 0)
                {
                    throw new Exception("必须输入IP地址或计算机名称.");
                }            if (UserID.Trim().Length > 0)
                {
                    co.Username = UserID;
                    co.Password = PassWord;
                }            ManagementScope ms = new ManagementScope("\\\\" + stringMachineName + "\\root\\cimv2", co);            oq = new ObjectQuery(stringQuery);
                query = new ManagementObjectSearcher(ms, oq);            try
                {
                    queryCollection = query.Get();
                }
                catch (ManagementException e)
                {
                    throw new Exception(e.Message);
                }            if (action != null)
                    foreach (ManagementObject o in queryCollection)
                        action(o);            return queryCollection;
            }        public string IP { get; set; }
            public string UserID { get; set; }
            public string PassWord { get; set; }
        }Utils utils = new Utils(ip或机器名,账户,密码)
    utils.getServiceCollection("Win32_NetworkAdapterConfiguration",
                        (mo) =>
                        {
                                 //mo["IPAddress"];
                        });