c#获取本机ip地址|获取本机的本地上网IP地址
具体方法为如下:
private void btnChenk_Click(object sender, System.EventArgs e)
{
System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
if ( addressList.Length>1)
{
nativeIP.Text = addressList[0].ToString();
serverIP.Text = addressList[1].ToString();
}
else
{
nativeIP.Text = addressList[0].ToString();
serverIP.Text = "Break the line...";
}
}
通常addressList[]所得到的列表就是当前的IP地址;
如果当前是拨号上网的话addressList[0].ToString()就是
本地IP地址;addressList[1].ToString() 就是上网地址。
如果当前是使用[EnterNet 300]拨号上网的话addressList[1].ToString()
就是本地IP地址;addressList[0].ToString() 就是上网地址。
如果程序只能够检测到只有一个IP地址的话,证明当前
电脑没有在线!
获取客户端IP:
Code
Code ![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
private void ButtonIP_Click(object sender, System.EventArgs e)
2
{
3
Syste m.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
4
if ( addressList.Length>1)
5
{
6
TextLIP.Text = addressList[0].ToString();
7
TextSIP.Text = addressList[1].ToString();
8
}
9
else
10
{
11
TextLIP.Text = addressList[0].ToString();TextSIP.Text = "没有可用的连接";
12
}
13
}
14![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System.Management;
string stringMAC = "";
string stringIP = "";
ManagementClass MC = new ManagementClass "Win32_NetworkAdapterConfiguration");
ManagementObjectCollection MOC= MC.GetInstances();
foreach(ManagementObject MO in MOC)
{
if ((bool)MO["IPEnabled"] == true){stringMAC += MO["MACAddress"].ToString();
TextMAC.Text = stringMAC.ToString();
string[] IPAddresses = (string[]) MO["IPAddress"];
if(IPAddresses.Length > 0)
stringIP = IPAddresses[0];TextIP.Text = stringIP.ToString();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
获取客户端MAC地址:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
获取服务器IP一:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
获取服务器IP及MAC地址二:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System.Management;
string stringMAC = "";
string stringIP = "";
ManagementClass MC = new ManagementClass "Win32_NetworkAdapterConfiguration");
ManagementObjectCollection MOC= MC.GetInstances();
foreach(ManagementObject MO in MOC)
{
if ((bool)MO["IPEnabled"] == true){stringMAC += MO["MACAddress"].ToString();
TextMAC.Text = stringMAC.ToString();
string[] IPAddresses = (string[]) MO["IPAddress"];
if(IPAddresses.Length > 0)
stringIP = IPAddresses[0];TextIP.Text = stringIP.ToString();
}
}
命运负责洗牌,但是玩牌的是我们自己!