//public string GetLocalIPv4(NetworkInterfaceType _type) //{ // // in order to get a wireless IPaddress use the follwoing code // //GetLocalIPv4(NetworkInterfaceType.Wireless80211); // //in order to get the ethernet ip address use the following code // //GetLocalIPv4(NetworkInterfaceType.Ethernet); // string output = ""; // foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces()) // { // if (item.NetworkInterfaceType == _type && item.OperationalStatus == OperationalStatus.Up) // { // foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses) // { // if (ip.Address.AddressFamily == AddressFamily.InterNetwork) // { // output = ip.Address.ToString(); // } // } // } // } // return output; //} public void RetSettings() { // Needs work, the NicCard method needs more data sent to it? string[] ipAddresses; string[] subnets; string[] gateways; string[] dnses; // Load current IP configuration for the selected NIC string nicName = (string)CboNic.SelectedItem; WMIHelper.GetIP(nicName, out ipAddresses, out subnets, out gateways, out dnses); // if network connection is disabled, no information will be available if (null == ipAddresses || null == subnets || null == gateways || null == dnses) { return; } ////Trim up the strings to more usable formats string strIP = string.Join(",", ipAddresses); string extIP = strIP.Substring(0, strIP.LastIndexOf(",")); TextIP.Text = extIP; LblCurrentIp.Text = "Current IP: " + extIP; string strSUB = string.Join(",", subnets); string extSUB = strSUB.Substring(0, strSUB.LastIndexOf(",")); TextSubnet.Text = extSUB; //string strGATE = string.Join(",", gateways); //string extGATE = strGATE.Substring(0, strGATE.LastIndexOf(",")); //TextGateway.Text = extGATE; // Show the setting //TextSubnet.Text = string.Join(",", subnets); TextGateway.Text = string.Join(",", gateways); TextDNS.Text = string.Join(",", dnses); // MessageBox.Show(string.Join(",", dchpstatus)); }
private void btnIPSet_Click(object sender, EventArgs e) { string[] ipaddrs, masks, gateways, dns; string currentIps = null; WMIHelper.GetIP(_wlnMgr.HostedNetworkInterfaceGuid, out ipaddrs, out masks, out gateways, out dns); ipaddrs = (ipaddrs ?? Enumerable.Empty <String>()).Where(_ => RegexIP.IsMatch(_)).ToArray(); if (ipaddrs.Any()) { currentIps = String.Join(" | ", ipaddrs); } bool ok = false; do { string ipAddr = Interaction.InputBox("输入IP地址:", currentIps == null ? "设置IP" : "当前IP:" + currentIps).Replace(" ", ""); if (ipAddr.Length == 0) { ok = true; } else { if (!RegexIP.IsMatch(ipAddr)) { this.Warn("IP地址格式不正确!"); ok = false; } else { WMIHelper.SetIP(_wlnMgr.HostedNetworkInterfaceGuid, ipAddr, "255.255.255.0", "", ""); this.Info("设置成功!"); ok = true; } } } while (!ok); }