C#获取主机IP地址
2008-10-04 09:18
309 查看
C#获取主机IP地址:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Collections;
namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.label1.Text=MyHostIP();
}
//获得主机IP地址
public string MyHostIP()
{
// 显示主机名
string hostname = Dns.GetHostName();
// 显示每个IP地址
IPHostEntry hostent = Dns.GetHostEntry(hostname); // 主机信息
Array addrs = hostent.AddressList; // IP地址数组
IEnumerator it = addrs.GetEnumerator(); // 迭代器,添加名命空间using System.Collections;
// 循环到下一个IP 地址
while (it.MoveNext())
{
IPAddress ip = (IPAddress)it.Current; //获得IP地址,添加名命空间using System.Net;
return ip.ToString();
}
return "";
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Collections;
namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.label1.Text=MyHostIP();
}
//获得主机IP地址
public string MyHostIP()
{
// 显示主机名
string hostname = Dns.GetHostName();
// 显示每个IP地址
IPHostEntry hostent = Dns.GetHostEntry(hostname); // 主机信息
Array addrs = hostent.AddressList; // IP地址数组
IEnumerator it = addrs.GetEnumerator(); // 迭代器,添加名命空间using System.Collections;
// 循环到下一个IP 地址
while (it.MoveNext())
{
IPAddress ip = (IPAddress)it.Current; //获得IP地址,添加名命空间using System.Net;
return ip.ToString();
}
return "";
}
}
}
相关文章推荐
- C# 获取主机IP地址
- c#获取主机名称网卡MACIP地址
- 【转】C#获取客户端及服务器端主机信息及其获取IP地址
- c#获取主机名称网卡MACIP地址
- C# 获取主机IP地址
- C# 获取电脑名IP地址及当前用户名
- MFC如何获取同一局域网中主机的IP地址
- C#根据计算机名获取对应IP地址
- C#获取本机所有IP地址(包括局域网和本机外网IP)
- C#.NET 获取电脑客户端IP地址及当前用户名
- 网络编程之获取主机名称与IP地址
- MFC如何获取自己电脑的IP地址和主机名
- C#获取网段内主机,C#调用系统程序
- UNIX环境编程-----Unix编程实例----获取本地主机IP地址等信息
- C# 网络编程之通过ip地址获取地理位置(补充)
- 根据获取的主机名显示ip地址
- C#中获取CPU序列号、网卡Mac地址、IP地址、硬盘序列号、登录用户名、PC类型、计算机名称、物理内存
- [C#] 在.NET(C#)中获取电脑名IP地址及当前用户名
- 获取主机信息(C++和C#版)
- socket编程之---------获取客户端主机名和IP地址