C# 从 LAN 中的主机名获取 IP 地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11931582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:45:46  来源:igfitidea点击:

Get IP address from hostname in LAN

c#windows-7network-programming

提问by houstonCYap

I found many samples on how to get a hostname by an IP address, how can I get the IP address of a host in the LAN?

我找到了很多关于如何通过 IP 地址获取主机名的示例,如何获取局域网中主机的 IP 地址?

采纳答案by januprasad

Try this

尝试这个

public static void DoGetHostAddresses(string hostname)
{

   IPAddress[] ips;

    ips = Dns.GetHostAddresses(hostname);

    Console.WriteLine("GetHostAddresses({0}) returns:", hostname);

    foreach (IPAddress ip in ips)
    {
        Console.WriteLine("    {0}", ip);
    }
}

i got this from http://msdn.microsoft.com/en-us/library/system.net.dns.gethostaddresses.aspx

我从http://msdn.microsoft.com/en-us/library/system.net.dns.gethostaddresses.aspx得到这个

回答by JleruOHeP

Here is an excellent example of how it is doing: http://www.codeproject.com/Articles/854/How-To-Get-IP-Address-Of-A-Machine

这是它如何工作的一个很好的例子:http: //www.codeproject.com/Articles/854/How-To-Get-IP-Address-Of-A-Machine

回答by Maciej

As long as you know the name of a machine, you can use Dns.GetHostAddresses. Your network DNS should recognize it as LAN computer and return proper IP.

只要您知道机器的名称,就可以使用Dns.GetHostAddresses. 您的网络 DNS 应将其识别为 LAN 计算机并返回正确的 IP。

回答by mtijn

you could use the windows management classes to do this, it also works for remote machines that are in the same domain (but I don't know if they need to enable or disable any security or policy settings for this to work). for example:

您可以使用 Windows 管理类来执行此操作,它也适用于同一域中的远程计算机(但我不知道他们是否需要启用或禁用任何安全或策略设置才能使其工作)。例如:

public List<NetworkAdapter> GetAdapterList()
{
    ManagementClass mgmt = new ManagementClass("Win32_NetworkAdapterConfiguration ");
    ManagementObjectCollection moc = mgmt.GetInstances();
    List<NetworkAdapter> adapters = new List<NetworkAdapter>();

    // Search for adapters with IP addresses
    foreach(ManagementObject mob in moc)
    {
        string[] addresses = (string[])mob.Properties["IPAddress"].Value;
        if (null == addresses)
        {
            continue;
        }

        NetworkAdapter na = new NetworkAdapter();
        na.Description = (string) mob.Properties["Description"].Value;
        na.MacAddress = (string) mob.Properties["MACAddress"].Value;
        na.IPAddresses = addresses;
        adapters.Add(na);
    }
    return adapters;
}

and to access a remote machine create the management class like this instead:

并访问远程机器,而是创建如下管理类:

ManagementClass mgmt = new ManagementClass
    (\\servername\root\cimv2:Win32_NetworkAdapterConfiguration);

this approach may get you more IPs than just the ones that have been registered in the DNS.

这种方法可能会为您提供更多的 IP,而不仅仅是在 DNS 中注册的 IP。

回答by SerG

Use Dns.GetHostEntry(hostname) instead of obsolete Dns.GetHostAddresses.

使用 Dns.GetHostEntry(hostname) 而不是过时的 Dns.GetHostAddresses。

回答by user2702347

Here is simple code if you want to get the IP Address(V4) from your pc.

如果您想从您的电脑获取 IP 地址(V4),这里是简单的代码。

Import this library into your class

将此库导入您的班级

using System.Net;

Initialize and declare these variables into your codes. They contain hostname, ipaddress and an array of Host Addresses:

将这些变量初始化并声明到您的代码中。它们包含主机名、IP 地址和一组主机地址:

string HostName = Dns.GetHostName().ToString();

IPAddress[] IpInHostAddress = Dns.GetHostAddresses(HostName);

string IPV4Address = IpInHostAddress[1].ToString(); //Default IPV4Address. This might be the ip address you need to retrieve

string IPV6Address = IpInHostAddress[0].ToString(); //Default Link local IPv6 Address

Open your command prompt, just type "ipconfig" and press enter.Once you are done, you could check if the string IPV4Address matches to IPv4Address in our pc.

打开您的命令提示符,只需键入“ipconfig”并按回车键。完成后,您可以检查字符串 IPV4Address 是否与我们电脑中的 IPv4Address 匹配。