none
在Win8系统下使用WMI修改IP失败 RRS feed

  • 问题

  • 以下这段代码是用WMI来实现修改本地的IP地址信息,使用.Net的环境是4.5,此程序在Win7上能成功修改IP信息,但是在Win8系统上,能成功运行,没有崩溃,没有错误提示,但是修改不了IP信息

    private void Setting_Click(object sender, RoutedEventArgs e)
            {
                if (MyNetCard == null)
                {
                    MessageBox.Show("获取所选的网卡失败!","获取网卡出错");
                    return;
                }
                try
                {
                    MyIP = NewIp.Text;
                    string StrGate = "";
                    ManagementBaseObject InPar = null;
                    ManagementBaseObject OutPar = null;
                    InPar = MyNetCard.GetMethodParameters("EnableStatic");
                    InPar["IPAddress"] = new string[] { MyIP };
                    InPar["SubnetMask"] = new string[] { "255.255.255.0" };
                    OutPar = MyNetCard.InvokeMethod("EnableStatic", InPar, null);
                    //Set IP
                    StrGate = "192.168.202.254";
                    InPar = MyNetCard.GetMethodParameters("SetGateways");
                    InPar["DefaultIPGateway"] = new string[] { StrGate };
                    OutPar = MyNetCard.InvokeMethod("SetGateways", InPar, null);
                    //Set Gateways
                    InPar = MyNetCard.GetMethodParameters("SetDNSServerSearchOrder");
                    InPar["DNSServerSearchOrder"] = new string[] { "202.96.128.86","202.38.192.33" };
                    OutPar = MyNetCard.InvokeMethod("SetDNSServerSearchOrder", InPar, null);
                    //Set DNS
                }
                catch(Exception ex) 
                {
                    MessageBox.Show("设置IP途中出错了!!\n"+ex.Message ,"设置IP出错!");
                }
            }
    寻求帮助,不知什么原因,Win8下修改不了IP信息,希望能找到原因和解决的方法。

    2014年6月10日 12:01

答案

  • 终于找到了原因了,在Win8下,用户权限问题,程序需要“以管理员身份运行”才能对IP进行设置。谢谢网友的热心回答!
    2014年6月17日 9:12

全部回复

  • 你好:

    首先请确保WMI的服务已经启动:

    http://msdn.microsoft.com/en-us/library/aa826517(v=vs.85).aspx

    尝试一下这个方法:

    public void setIP(string ip_address, string subnet_mask)
            {
                ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection objMOC = objMC.GetInstances();
    
                foreach (ManagementObject objMO in objMOC)
                {
                    if ((bool)objMO["IPEnabled"])
                    {
                        try
                        {
                            ManagementBaseObject setIP;
                            ManagementBaseObject newIP =
                                objMO.GetMethodParameters("EnableStatic");
    
                            newIP["IPAddress"] = new string[] { ip_address };
                            newIP["SubnetMask"] = new string[] { subnet_mask };
    
                            setIP = objMO.InvokeMethod("EnableStatic", newIP, null);
                        }
                        catch (Exception)
                        {
                            throw;
                        }
    
    
                    }
                }
            }
    


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年6月11日 8:30
    版主
  • 你好,谢谢你的回复。

    我用过你的代码,在win8上修改IP还是没有效果,没有错误提示,但是在win7上能修改。

    还有一点,我做的程序是WPF程序,里面写了一段用WMI查询IP的方法,在Win7和Win8上都能成功运行并返回正确的IP信息,所以我相信Win8上的WMI是开启了的,但是不知为何,就是修改不了IP。

    这里附上查询IP的代码,和检测网卡的代码

     private void NetCard_Loaded(object sender, RoutedEventArgs e)
            {
                try
                {
                    int i=0;
                    string cardName = "";
                    ManagementClass mc = new ManagementClass("Win32_NetWorkAdapterConfiguration");
                    ManagementObjectCollection moc = mc.GetInstances();
                    NetCards = new ManagementObject[moc.Count];
                    foreach (ManagementObject mo in moc)
                    {
                        if ((bool)mo["IPEnabled"])
                        {
                            cardName = mo["Description"].ToString().Trim();
                            NetCard.Items.Add(cardName);
                            NetCards[i++] = mo;
                        }
                    }
                    NetCard.SelectedIndex = 0;
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message ,"提示");
                }
            }
            private void NetCard_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                if (NetCards == null) return;
                try
                {
                    string s = NetCard.SelectedItem.ToString();
                    foreach (ManagementObject mo in NetCards)
                    {
                        if (s == mo["Description"].ToString().Trim())
                        {
                            MyNetCard = new ManagementObject();
                            MyNetCard = mo;
                            break;
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("选取网卡信息失败!");
                }
            }
            private void GetIPInfo()
            {
                try
                {
                    TextShow.Text += "----------以下是当前的IP信息----------\n";
                    ManagementClass mc = new ManagementClass("Win32_NetWorkAdapterConfiguration");
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        if (!(bool)mo["IPEnabled"])
                            continue;
                        if (NetCard.SelectedItem.ToString() == mo["Description"].ToString().Trim())
                        {
                            string[] IPAdress = (string[])mo["IPAddress"];
                            TextShow.Text += "IP地址为:" + IPAdress[0] + '\n';
                            if (IPAdress [0]!="0.0.0.0")
                            {
                                string[] subnet = (string[])mo["IPSubnet"];
                                TextShow.Text += "子掩码为:" + subnet[0] + "\n";
                            }
                            else    MessageBox .Show ("获取IP地址为空!","提示");
                            string[] gatestr=(string[])mo["DefaultIPGateway"];
                            TextShow.Text += "网关为:" + gatestr[0] + "\n";
                            string[] DnsList = (string[])mo["DNSServerSearchOrder"];
                            TextShow.Text += "DNS为:"+DnsList [0]+"\n";
                            if (DnsList.Length > 1)
                                TextShow.Text += "备用DNS为:" + DnsList[1] + "\n";
                        }
                    }
                    TextShow.Text += "-------以上是当前的IP信息-------\n";
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message ,"获取IP信息时出错!");
                }
            }

    希望能找出问题所在。
    2014年6月11日 9:27
  • 终于找到了原因了,在Win8下,用户权限问题,程序需要“以管理员身份运行”才能对IP进行设置。谢谢网友的热心回答!
    2014年6月17日 9:12