asp.net C # Get native IP address instance code

Source: Internet
Author: User
Tags split

Example 1

The code is as follows Copy Code

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Net;

Namespace Csharptest
{
Class Program
{
static void Main (string[] args)
{
Console.Write (new program (). GetHostInfo ());
Console.ReadLine ();
}
Get information such as local IPD
Protected string GetHostInfo ()
{
StringBuilder hostinfo = new StringBuilder ("");
ipaddress[] Iphost = dns.gethostaddresses (Dns.gethostname ());
Hostinfo.append ("This machine Name:");
Hostinfo.append (Dns.gethostname ());
Hostinfo.append ("RN");
Hostinfo.append ("IP address:");
Hostinfo.append ("RN");

foreach (IPAddress address in iphost)
{
Hostinfo.append (address. ToString ());
Hostinfo.append ("RN");
}

return hostinfo.tostring ();
}
}
}


Console output Results:

This machine name: meteor-pc
IP Address:
Fe80::a1a2:949c:1a51:5e6c%11
2002:b46f:20fa::b46f:20fa
169.254.94.108
180.111.32.250

Example 2

The code is as follows Copy Code


public static IPAddress Gethostip ()
{
Iphostentry ipe = Dns.gethostentry (Dns.gethostname ());
IPAddress IP = ipe. ADDRESSLIST[0];


return IP;
}
Private String getipaddress ()
{
String str;
String result = "";
String hostName = Dns.gethostname ();
ipaddress[] MyIP = dns.gethostaddresses (hostName);
foreach (IPAddress address in MyIP)
{
str = address. ToString ();
for (int i = 0; i < str. Length; i++)
{
if (Str[i] >= ' 0 ' && str[i] <= ' 9 ' | | | str[i] = = '. ') result = STR;
}
}
return result;
}

Example 3

The above method can not meet my needs, and then I search in Google some, and there is no good way, so I groped to write down this method. If you think there's something wrong

The code is as follows Copy Code

<summary>
Get native IP
</summary>
private String Getlocalip ()
{
Native IP Address
String strlocalip = "";
Get the computer name
String strpcname = Dns.gethostname ();
Get an array of native IP addresses
Iphostentry ipentry = Dns.gethostentry (strpcname);
Traversing an array
foreach (Var ipadd in ipentry.addresslist)
{
Determines whether the current string is the correct IP address
if (Isrightip (ipadd.tostring ()))
{
Get local IP Address
Strlocalip = Ipadd.tostring ();
End Loop
Break
}
}

Return local IP Address
return strlocalip;
}
  

?//Get gateway Address
private String Getgateway ()
{
Gateway Address
String strgateway = "";
Get all network adapters
networkinterface[] nics = networkinterface.getallnetworkinterfaces ();
Traversing an array
foreach (Var network in NICs)
{
IP object for a single network adapter
Ipinterfaceproperties IP = network.getipproperties ();
Gets the gateway for this IP object
gatewayipaddressinformationcollection gateways = IP. gatewayaddresses;
foreach (Var gateWay in gateways)
{
If you can ping through the gateway
if (ISPINGIP (gateWay.Address.ToString ()))
{
Get gateway Address
Strgateway = GateWay.Address.ToString ();
Jump out of the loop
Break
}
}

If you've got the gateway address
if (Strgateway.length > 0)
{
Jump out of the loop
Break
}
}

Return to Gateway address
return strgateway;
}
  

?///<summary>
Determine if it is the correct IP address
</summary>
<param name= "Stripadd" > Need to judge the string </param>
<returns>true = is false = no </returns>
public static bool Isrightip (string stripadd)
{
Using regular expressions to determine whether a string conforms to IPV4 format
if (Regex.IsMatch (Stripadd, "[0-9]{1,3}\.[ 0-9]{1,3}\. [0-9] {1,3}\. [0-9] {1,3} "))
{
Split string by decimal point
string[] ips = Stripadd.split ('. ');
if (IPs. Length = = 4 | | Ips. Length = = 6)
{
If the IPV4 rule is met
if (System.Int32.Parse (Ips[0]) < 256 && System.Int32.Parse (ips[1)) < 256 & System.Int32.Parse (ips[2)) < 256 & System.Int32.Parse (Ips[3]) < 256)
That's right
return true;
If you do not meet
Else
Error
return false;
}
Else
Error
return false;
}
Else
Error
return false;
}
  

?///<summary>
///Attempt to ping specifies whether IP can ping
///</summary>
///<param name= "StrIP" > Specify IP< ;/param>
///<returns>true is false </returns>
public static bool Ispingip (string StrIP)   {
    try
    {
       //Create Ping Object
        Ping ping = new ping ();
       //Accept ping return value
        pingreply reply = ping. Send (StrIP, 1000);
       //ping Pass
        return true;
   }
    catch
    {
       //ping failure
& nbsp;       return false;
   }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.