查找本地服务器的IP地址
我想动态检索本地网络上服务器的 IP 地址。我如何检索这些 IP 地址?
使用答案中的代码进行更新:
// Query for all the enabled network adapters
ManagementObjectSearcher objSearcher = new ManagementObjectSearcher(
"SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled='TRUE'");
ManagementObjectCollection objCollection = objSearcher.Get();
// Loop through all available network interfaces
foreach (ManagementObject obj in objCollection)
{
// List all IP addresses of the current network interface
string[] AddressList = (string[])obj["IPAddress"];
foreach (string Address in AddressList)
{
MessageBox.Show(Address);
}
}
我使用这个代码,但它只返回我自己电脑的IP地址,而不是网络中的所有IP。
I would like to retrieve the IP addresses of the server (or servers) on the local network dynamically. How can I retrieve these IP addresses?
Update with code from an answer:
// Query for all the enabled network adapters
ManagementObjectSearcher objSearcher = new ManagementObjectSearcher(
"SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled='TRUE'");
ManagementObjectCollection objCollection = objSearcher.Get();
// Loop through all available network interfaces
foreach (ManagementObject obj in objCollection)
{
// List all IP addresses of the current network interface
string[] AddressList = (string[])obj["IPAddress"];
foreach (string Address in AddressList)
{
MessageBox.Show(Address);
}
}
I use this code but it only returns my own PC's IP address, not all the IPs in network.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可能相当困难,具体取决于网络的配置。如果它只是一个 Windows 网络,并且您运行应用程序的帐户具有管理员权限,那么会更容易一些。
最好的方法是查询您的 PDC(主域控制器)。查看 System.DirectoryServices.ActiveDirectory 命名空间。
如果我没记错的话,你可以使用LDAP来查询域控制器——只要PDC配置正确!我发现这个 LDAP 查询可能对您有帮助:
“(&(objectCategory=computer)(|(operatingSystem=Windows Server*)(operatingSystem=Windows 2000 Server))))))”
当然,这只会查询 windows 2000服务器 - 您应该能够根据需要进行修改。
查看以下链接:
http://www.google.co.uk/search?gcx=c&sourceid=chrome&ie=UTF-8&q=c%23+ldap+query
This could be quite difficult, depending on the configuration of the network. If it's solely a windows network, and the account you're running the application as has Administrator rights, it'll be a bit easier.
The best way would be to query your PDC (Primary Domain Controller). Check out the System.DirectoryServices.ActiveDirectory namespace.
If I remember correctly, you can use LDAP to query the domain controller - as long as the PDC is correctly configured! I found this LDAP query that may help you:
"(&(objectCategory=computer)(|(operatingSystem=Windows Server*)(operatingSystem=Windows 2000 Server))))))"
Of course, that will only query windows 2000 servers - you should be able to modify as needed.
Check out the following links:
http://www.google.co.uk/search?gcx=c&sourceid=chrome&ie=UTF-8&q=c%23+ldap+query
此可让您通过名称获取计算机的 IP 地址。这就是您要找的吗?
This will let you get the IP address of a machine by name. Is that what you are looking for?
您是否看过 Apple 的 Bonjour (Zeroconf) 等发现协议。 http://en.wikipedia.org
Have you looked at a discovery protocol such as Apple's Bonjour (Zeroconf). http://en.wikipedia.org