Obtain the local IP address and MAC address

Source: Internet
Author: User

Writing "using remoting technology to push messages", in which messages are pushed to the client. At this time, you must know some information about the client, such as the IP address and port. Here, we first obtain the IP address and MAC address informationCodePaste it.ArticleWait until I have time to write it again.

/**/ ///   <Summary>
/// Obtain the MAC address of the Local Computer
///   </Summary>
///   <Returns> MAC address </Returns>
///   <Remarks>
/// Obtain the MAC address of the Local Computer
///   </Remarks>
Static   Public   String Getmacaddress ()
{
String Strmac =   String . Empty;
Managementclass MC =   New Managementclass ( " Win32_networkadapterconfiguration " );
Managementobjectcollection MoC = MC. getinstances ();

Foreach (Managementobject Mo In MOC)
{
If (( Bool ) Mo [ " Ipenabled " ] =   True )
{
Strmac+ =Mo ["Macaddress"]. Tostring ();
}
}
Return Strmac;
}  

/**/ ///   <Summary>
/// Obtain the IP address of the Local Computer
///   </Summary>
///   <Returns> IP address </Returns>
///   <Remarks>
/// Obtain the IP address of the Local Computer
///   </Remarks>
Static   Public   String Getipaddress ()
{
String Strip =   String . Empty;
Managementclass MC =   New Managementclass ( " Win32_networkadapterconfiguration " );
Managementobjectcollection MoC = MC. getinstances ();

Foreach (Managementobject Mo In MOC)
{
If (( Bool ) Mo [ " Ipenabled " ] =   True )
{
String [] Ipaddresses = ( String []) Mo [ " IPaddress " ];
If (Ipaddresses. Length >   0 )
Strip = Ipaddresses [ 0 ];
}
}
Return Strip;
}  

/**/ ///   <Summary>
/// Obtain the local computer name
///   </Summary>
///   <Returns> Local Computer Name </Returns>
///   <Remarks>
/// Obtain the local computer name
///   </Remarks>
Static   Public   String Gethostname ()
{
String Hostname =   "" ;
Try
{
Hostname=DNS. gethostname ();
}
Catch
{
}
Return Hostname;
}

If you want to scan the Internet or want to know the MAC address of a machine in the same network segment, you need to change the method.

Thanks for the hbifts reminder. The code for using DNS to obtain the IP address is provided for your reference:

Public   Static   Void Getipaddress ()
{< br> iphostentry hostinfo = DNS. resolve (DNS. gethostname ();
IPaddress srcaddress = hostinfo. addresslist [ 0 ];
return srcaddress. tostring ();
}

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.