一:C# 根据主机名称获得IP;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace GetIp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string name= Dns.GetHostName();
// IPHostEntry Iphost= Dns.GetHostByName(name);
// IPAddress Ip = Iphost.AddressList[0];
// textBox1.Text = Ip.ToString();
IPHostEntry en= Dns.GetHostEntry(name); //获得主机名称;
for(int i = 0; i<en.AddressList.Length; i++)
{
textBox1.AppendText(en.AddressList[i].ToString()+"\r\n"); // 获得IP;
}
// textBox1.Text = en.AddressList[2].ToString();
}
private void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}