C# 局域网内通过电脑名获取对应IP 怎么实现???
发布网友
发布时间:2022-04-27 11:14
我来回答
共3个回答
热心网友
时间:2023-08-21 17:20
using System;
using System.Collections.Generic;
using System.Net;
namespace GetMachineIP
{
class Program
{
static List<string> GetIPByHostName(string strHostName)
{
// Then using host name, get the IP address list..
IPHostEntry ipEntry = Dns.GetHostByName(strHostName);
IPAddress[] addr = ipEntry.AddressList;
List<string> ips = new List<string>();
for (int i = 0; i < addr.Length; i++)
{
ips.Add(addr[i].ToString());
}
return ips;
}
public static int Main(string[] args)
{
String strHostName = "";
if (args.Length == 0)
{
// Getting Ip address of local machine...
// First get the host name of local machine.
strHostName = Dns.GetHostName();
Console.WriteLine("Local Machine's Host Name: " + strHostName);
}
else
{
strHostName = args[0];
}
var ips = GetIPByHostName(strHostName);
foreach (var ip in ips)
{
Console.WriteLine(ip);
}
return 0;
}
}
}追问是局域网内,通过电脑名或者其他固定的条件方式获取其他电脑对应的IP
热心网友
时间:2023-08-21 17:21
static System.Net.Dns.Resolve(电脑名).AddressList
热心网友
时间:2023-08-21 17:21
IPHostEntry hostEntry;
hostEntry= Dns.GetHostEntry(host);
if (hostEntry.AddressList.Length > 0)
{ var ip = hostEntry.AddressList[0];
}