C# 轮询多个设备以确定 IP 类型:静态或 DHCP
想知道实际检索 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,这是不可能的。这肯定比我更接近。我坚信这是可以实现的。我愿意接受这里的建议。
Would like to know the c# code to actually retrieve the IP type: Static or DHCP based on a list of devices I will enter.
Output to be viewed:
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());
This gave me 90% of what I am looking to achieve - the code worked out well. The next portion is to specify a device on the network to obtain the information remotely. I noticed the one comment below that stated this was impossible without WMI. This is certainly much closer than I was. I'm convinced this can be accomplished. I'm open to recommendations here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑:抱歉,某些适配器上的某些属性可能为 NULL。已修复如下
EDIT: sorry, some properties can be NULL on some adapters. Fixed below
看看 http://www.codeguru.com/csharp /csharp/cs_network/internetweb/article.php/c6023/
希望这有帮助
Look at http://www.codeguru.com/csharp/csharp/cs_network/internetweb/article.php/c6023/
hope this helps
如果您尝试对远程主机执行此操作,您将无法执行此操作。您需要访问 DHCP 服务器及其日志记录来识别此信息。编辑:当然,如果可用,可以通过 WMI 进行操作。
If you're trying to do this for remote hosts, you won't be able to do this. You'll need access to the DHCP server and its logging to identify this information.Edit: of course, through WMI works if that's available.