查找本地服务器的IP地址

发布于 2024-10-18 12:31:23 字数 841 浏览 2 评论 0原文

我想动态检索本地网络上服务器的 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);
   }
}

code source

I use this code but it only returns my own PC's IP address, not all the IPs in network.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你怎么敢 2024-10-25 12:31:23

这可能相当困难,具体取决于网络的配置。如果它只是一个 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

风流物 2024-10-25 12:31:23

可让您通过名称获取计算机的 IP 地址。这就是您要找的吗?

This will let you get the IP address of a machine by name. Is that what you are looking for?

执妄 2024-10-25 12:31:23

您是否看过 Apple 的 Bonjour (Zeroconf) 等发现协议。 http://en.wikipedia.org

Have you looked at a discovery protocol such as Apple's Bonjour (Zeroconf). http://en.wikipedia.org

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文