C#如何获取本机的ip地址?

rainraid 2005-09-27 01:06:12
如题。用什么函数啊?
...全文
20917 36 打赏 收藏 转发到动态 举报
写回复
用AI写文章
36 条回复
切换为时间正序
请发表友善的回复…
发表回复
大个啊 2006-06-22
  • 打赏
  • 举报
回复
winform至少有三种方式可以获取:
注册表
WMI
DNS
alang_jj 2006-02-06
  • 打赏
  • 举报
回复
比较复杂的问题,看看了
qiezicc 2006-02-06
  • 打赏
  • 举报
回复
mark
Yuna_2z 2006-02-06
  • 打赏
  • 举报
回复
靠...要看IP(句于网的!)cmd->ipconfig - _ -!
thomaslw 2006-02-06
  • 打赏
  • 举报
回复
本地地址:

关键是这两句
IPHostEntry ihe = Dns.GetHostByName(Dns.GetHostName());
IPAddress myself = ihe.AddressList[0];

============================================================
外网地址:

还是只有 wuyi8808(空间/IV) 的程序正确.
wlwsoft 2006-02-06
  • 打赏
  • 举报
回复
Mark
eagletloki 2006-02-06
  • 打赏
  • 举报
回复
Mark
止戈而立 2006-01-10
  • 打赏
  • 举报
回复
mark
xiangyuen 2006-01-10
  • 打赏
  • 举报
回复
不会,学习,做个记标记
chenyu17094256 2005-09-28
  • 打赏
  • 举报
回复
IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName());

所有IP都在ipHost.AddressList列表中
LoveMango 2005-09-28
  • 打赏
  • 举报
回复
mark
wuyi8808 2005-09-28
  • 打赏
  • 举报
回复
helloworld84(初吻给了莉)的方法可行, 我把它翻译成C#的:

// 通过外部网站返回得到内网外部IP
// 来源: http://www.cnblogs.com/helloworld84/archive/2005/08/18/218104.html

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

class PubIP
{
static string GetIP()
{
Uri uri = new Uri("http://www.ikaka.com/ip/index.asp");
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = 0;
req.CookieContainer = new CookieContainer();
req.GetRequestStream().Write(new byte [0], 0, 0);
HttpWebResponse res = (HttpWebResponse)(req.GetResponse());
StreamReader rs = new StreamReader(res.GetResponseStream(), Encoding.GetEncoding("GB18030"));
string s = rs.ReadToEnd();
rs.Close();
req.Abort();
res.Close();
Match m = Regex.Match(s, @"IP:\[(?<IP>[0-9\.]*)\]");
if (m.Success) return m.Groups["IP"].Value;
return string.Empty;
}

static void Main()
{
Console.WriteLine(GetIP());
}
}
commandown 2005-09-28
  • 打赏
  • 举报
回复
可以得到,但要通过公网的服务器才行~
zeusvenus 2005-09-28
  • 打赏
  • 举报
回复
看看我的blog上关于ASP.NET获取IP与MAC的帖子:
http://www.cnblogs.com/Zeus/archive/2005/04/20/141502.aspx
songyuan 2005-09-28
  • 打赏
  • 举报
回复
mark
dragonfly001 2005-09-28
  • 打赏
  • 举报
回复
marking
yynice 2005-09-28
  • 打赏
  • 举报
回复
上面的改成C#就更好了
Arucueid 2005-09-28
  • 打赏
  • 举报
回复
mark
mmens 2005-09-28
  • 打赏
  • 举报
回复
Mark
知识2018 2005-09-28
  • 打赏
  • 举报
回复
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Management;
using System.Net;
using System.Net.Sockets ;


private void button1_Click(object sender, System.EventArgs e)
{
string s="",mac="";
//利用DNS组件的GetHostName函数读取机器名
string hostInfo=Dns.GetHostName();
//读取IP地址列表
System.Net.IPAddress [] addressList=Dns.GetHostByName(Dns.GetHostName()).AddressList;
for(int i=0;i<addressList.Length;i++)
{
s+=":" + addressList[i].ToString();
}
//利用ManagementClass类取得MAC地址
ManagementClass mc;
mc=new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc=mc.GetInstances();
foreach(ManagementObject mo in moc)
{
if (mo ["IPEnabled"].ToString ()=="True")
mac=mo["MacAddress"].ToString();
}
//在广本编辑框中显示数据
ComputerName.Text=hostInfo;
IpAdd.Text=s;
MacAdd.Text=mac;
}
加载更多回复(16)

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧