想知道实际检索IP类型的c#代码:基于我将输入的设备列表的静态或DHCP。
要查看的输出:
Device name: IP Address: MAC Address: Type: Marvell Yukon 88E8001/8003/8010 PCI Gigabit Ethernet Controller NULL 00:00:F3:44:C6:00 DHCP Generic Marvell Yukon 88E8056 based Ethernet Controller 192.168.1.102 00:00:F3:44:D0:00 DHCP
ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();
txtLaunch.Text = ("Name\tIP Address\tMAC Address\tType" +"\r\n");
foreach (ManagementObject objMO in objMOC)
{
StringBuilder builder = new StringBuilder();
object o = objMO.GetPropertyValue("IPAddress");
object m = objMO.GetPropertyValue("MACAddress");
if (o != null || m != null)
{
builder.Append(objMO["Description"].ToString());
builder.Append("\t");
if (o != null)
builder.Append(((string[])(objMO["IPAddress"]))[0].ToString());
else
builder.Append("NULL");
builder.Append("\t");
builder.Append(m.ToString());
builder.Append("\t");
builder.Append(Convert.ToBoolean(objMO["DHCPEnabled"]) ? "DHCP" : "Static");
builder.Append("\r\n");
}
txtLaunch.Text = txtLaunch.Text + (builder.ToString());
这给了我90%的目标 - 代码效果很好。下一部分是指定网络上的设备以远程获取信息。我注意到下面的一条评论说没有WMI这是不可能的。这肯定比我更接近。我确信这可以实现。我愿意接受这里的建议。
答案 0 :(得分:2)
编辑:抱歉,某些适配器上的某些属性可能为NULL。修正了
ManagementClass objMC = new ManagementClass(
"Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();
Console.WriteLine("Name\tIP Address\tMAC Address\tType");
foreach (ManagementObject objMO in objMOC)
{
StringBuilder builder = new StringBuilder();
builder.Append(objMO["Description"].ToString());
builder.Append("\t");
object o = objMO.GetPropertyValue("IPAddress");
if (o != null)
builder.Append(((string[])(objMO["IPAddress"]))[0].ToString());
else
builder.Append("NULL");
builder.Append("\t");
object m = objMO.GetPropertyValue("MACAddress");
if (m != null)
builder.Append(m.ToString());
else
builder.Append("NULL");
builder.Append("\t");
builder.Append(Convert.ToBoolean(objMO["DHCPEnabled"]) ? "DHCP" : "Static");
Console.WriteLine(builder.ToString());
}
答案 1 :(得分:0)
答案 2 :(得分:0)
如果您尝试为远程主机执行此操作,则无法执行此操作。您需要访问DHCP服务器及其日志记录以识别此信息。
编辑:当然,如果可以的话,通过WMI工作。