日期:2014-05-17 浏览次数:21328 次
public bool ModIP(ip myIP) { bool isSuccess = false; ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if ((bool)mo["IPEnabled"]) { myIP.Mac = mo["MacAddress"].ToString(); try { //设置IP地址和子网掩码 ManagementBaseObject newIPSubMask = mo.GetMethodParameters("EnableStatic"); newIPSubMask["IPAddress"] = new string[] { myIP.Ip }; newIPSubMask["SubnetMask"] = new string[] { myIP.Subnetmask }; ManagementBaseObject setIPSubMask = mo.InvokeMethod("EnableStatic", newIPSubMask, null); //设置 //ManagementBaseObject newSubMask = mo.GetMethodParameters("EnableStatic"); //newSubMask["SubnetMask"] = new string[] { myIP.Subnetmask }; //ManagementBaseObject setSubMask = mo.InvokeMethod("EnableStatic", newSubMask, null); //设置网关 ManagementBaseObject newGate = mo.GetMethodParameters("SetGateways"); newGate["DefaultIPGateway"] = new string[] { myIP.Gateway }; ManagementBaseObject setGateway = mo.InvokeMethod("SetGateways", newGate, null); //设置DNS ManagementBaseObject newDNS = mo.GetMethodParameters("SetDNSServerSearchOrder"); newDNS["DNSServerSearchOrder"] = new string[] { myIP.Dns1, myIP.Dns2 }; ManagementBaseObject setDNS = mo.InvokeMethod("SetDNSServerSearchOrder", newDNS, null); isSuccess = true; } catch (Exception ex) { MessageBox.Show("原因:" + ex); isSuccess = false; } } } return isSuccess; }
string carName = ""; ManagementObjectSearcher search = new ManagementObjectSearcher ("SELECT * FROM Win32_NetWorkAdapterConfiguration"); foreach (ManagementObject sear in search.Get()) { if (sear["IPAddress"] != null) { carName = sear["Description"].ToString().Trim(); comboBox1.Items.Add(carName); } } comboBox1.SelectedIndex = 0;