我创建了一个winform应用程序,可以使用C#检测,设置和切换IPv4设置。当用户想要从DHCP自动获取IP时,我呼叫Automatic_IP()
:
Automatic_IP:
private void Automatic_IP()
{
ManagementClass mctemp = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moctemp = mctemp.GetInstances();
foreach (ManagementObject motemp in moctemp)
{
if (motemp["Caption"].Equals(_wifi)) //_wifi is the target chipset
{
motemp.InvokeMethod("EnableDHCP", null);
break;
}
}
MessageBox.Show("IP successfully set automatically.","Done!",MessageBoxButtons.OK,MessageBoxIcon.Information);
Getip(); //Gets the current IP address, subnet, DNS etc
Update_current_data(); //Updates the current IP address, subnets etc into a labels
}
在Getip
方法中,我提取当前的IP地址,子网,网关和DNS,无论所有这些都可以由DHCP手动设置或自动分配,并使用标签更新标签中的值。 Update_current_data()
方法。
Getip:
public bool Getip()
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if(!chipset_selector.Items.Contains(mo["Caption"]))
chipset_selector.Items.Add(mo["Caption"]);
if (mo["Caption"].Equals(_wifi))
{
ipadd = ((string[])mo["IPAddress"])[0];
subnet = ((string[])mo["IPSubnet"])[0];
gateway = ((string[])mo["DefaultIPGateway"])[0];
dns = ((string[])mo["DNSServerSearchOrder"])[0];
break;
}
}
}
但问题是我无法检测当前IP是手动设置还是自动分配,但我可以从Automatic_IP
方法中选择从DHCP 自动选择。 ManagementObject.InvokeMethod("EnableDHCP", null);
可以轻松地将其设置为自动获取IP地址但我无法检查应用程序首次启动时是自动还是手动设置IP。
我做了一些挖掘,发现了类似this的帖子。虽然存在一个非常相似的帖子here,但这是关于DNS而不是IP设置。
基本上我想找到选择的选项:
答案 0 :(得分:4)
ManagementObject
类有一堆properties可以使用,对于网络适配器,其中一个叫DHCPEnabled
。这告诉您网络接口是否自动获取IP地址。例如:
var isDHCPEnabled = (bool)motemp.Properties["DHCPEnabled"].Value;
答案 1 :(得分:2)
使用NetworkInformation类的替代方法:
using System.Net.NetworkInformation;
NetworkInterface[] Interfaces = NetworkInterface.GetAllNetworkInterfaces();
IPInterfaceProperties IPProperties = Interfaces[0].GetIPProperties();
IPv4InterfaceProperties IpV4Properties = IPProperties.GetIPv4Properties();
bool DhcpEnabed = IpV4Properties.IsDhcpEnabled;
启用DHCP的所有网络接口及其IP地址:
List<NetworkInterface> DhcpInterfaces = Interfaces
.Where(IF => (IF.Supports(NetworkInterfaceComponent.IPv4)) &&
(IF.GetIPProperties().GetIPv4Properties().IsDhcpEnabled))
.ToList();
if (DhcpInterfaces != null)
{
IEnumerable<UnicastIPAddressInformation> IpAddresses =
DhcpInterfaces.Select(IF => IF.GetIPProperties().UnicastAddresses.Last());
}
一个示例类,用于收集和检查NetworkInformation
类可以提供的一些(有限数量)信息:
using System.Net.NetworkInformation;
public class NetWorkInterfacesInfo
{
public string Name { get; set; }
public string Description { get; set; }
public PhysicalAddress MAC { get; set; }
public List<IPAddress> IPAddresses { get; set; }
public List<IPAddress> DnsServers { get; set; }
public List<IPAddress> Gateways { get; set; }
public bool DHCPEnabled { get; set; }
public OperationalStatus Status { get; set; }
public NetworkInterfaceType InterfaceType { get; set; }
public Int64 Speed { get; set; }
public Int64 BytesSent { get; set; }
public Int64 BytesReceived { get; set; }
}
List<NetWorkInterfacesInfo> NetInterfacesInfo = new List<NetWorkInterfacesInfo>();
NetInterfacesInfo = DhcpInterfaces.Select(IF => new NetWorkInterfacesInfo()
{
Name = IF.Name,
Description = IF.Description,
Status = IF.OperationalStatus,
Speed = IF.Speed,
InterfaceType = IF.NetworkInterfaceType,
MAC = IF.GetPhysicalAddress(),
DnsServers = IF.GetIPProperties().DnsAddresses.Select(ipa => ipa).ToList(),
IPAddresses = IF.GetIPProperties().UnicastAddresses.Select(ipa => ipa.Address).ToList(),
Gateways = IF.GetIPProperties().GatewayAddresses.Select(ipa => ipa.Address).ToList(),
DHCPEnabled = IF.GetIPProperties().GetIPv4Properties().IsDhcpEnabled,
BytesSent = IF.GetIPStatistics().BytesSent,
BytesReceived = IF.GetIPStatistics().BytesReceived
}).ToList();
统计特征:
IPGlobalStatistic
TcpConnectionInformation
IPInterfaceStatistics
活动:
NetworkChange
→NetworkAddressChanged
→NetworkAvailabilityChanged
公用设施:
Ping