C # obtain the local IP address and MAC address

Source: Internet
Author: User
1. Obtain the lan ip address through the host name; try {iphostentry = DNS. gethostentry (txthost. text); // pass the computer name if (iphostentry. aliases. length> 0) {foreach (string alias in iphostentry. aliases) txtip. TEXT = alias;} // obtain the IP address foreach (IPaddress ADDR in iphostentry. addresslist) txtipaddress. TEXT = ADDR. tostring (); // get the IP address} catch {MessageBox. show ("Incorrect host name. ");} 2. Obtain the host name through the lan ip address; try {iphostentry = DNS. gethostbyaddress (txtip. text); // transmit the IP address txthostname. TEXT = iphostentry. hostname. tostring () // get the host name} catch {MessageBox. show ("Incorrect IP address. ");} 3. Obtain the MAC address public static string readmac (string IP) through the lan ip address // you can return the MAC address {string MAC =" "; system. diagnostics. PROCESS p = new system. diagnostics. process (); p. startinfo. filename = "NBTSTAT"; p. startinfo. arguments = "-a" + IP; p. startinfo. useshellexecute = false; p. startinfo. createnowindow = true; p. startinfo. redirectstandardoutput = true; p. start (); string output = P. standardoutput. readtoend (); int Len = output. indexof ("MAC address ="); If (LEN> 0) {MAC = output. substring (LEN + 14, 17);} p. waitforexit (); Return MAC;

}

 

C # obtain the local computer name, IP address, and MAC address

Using system;
Using system. drawing;
Using system. Management;
Using system.net;
Using system. net. Sockets;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;

Namespace mac_ip_name
{
/// <Summary>
/// Summary of form1.
/// </Summary>
Public class form1: system. Windows. Forms. Form
{
Private system. Windows. Forms. Label label4;
Private system. Windows. Forms. groupbox groupbox1;
Private system. Windows. Forms. textbox txtmac;
Private system. Windows. Forms. textbox txtip;
Private system. Windows. Forms. textbox txtname;
Private system. Windows. Forms. Label lblmac;
Private system. Windows. Forms. Label lblip;
Private system. Windows. Forms. Label lblname;
Private system. Windows. Forms. Button button1;
Private system. Windows. Forms. Button button2;
/// <Summary>
/// Required designer variables.
/// </Summary>
Private System. ComponentModel. Container components = null;

Public Form1 ()
{
//
// Required for Windows Form Designer support
//
InitializeComponent ();

//
// Todo: add Any constructor code after initializecomponent calls
//
}

/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = Null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}

# Region code generated by Windows Form Designer
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void InitializeComponent ()
{
This. label4 = new System. Windows. Forms. Label ();
This. groupBox1 = new System. Windows. Forms. GroupBox ();
This. button2 = new System. Windows. Forms. Button ();
This.txt MAC = new system. Windows. Forms. Textbox ();
This.txt IP = new system. Windows. Forms. Textbox ();
This.txt name = new system. Windows. Forms. Textbox ();
This. lblmac = new system. Windows. Forms. Label ();
This. lblip = new system. Windows. Forms. Label ();
This. lblname = new system. Windows. Forms. Label ();
This. button1 = new system. Windows. Forms. Button ();
This. groupBox1.SuspendLayout ();
This. SuspendLayout ();
//
// Label4
//
This. label4.AutoSize = true;
This. label4.Font = new System. drawing. font (" _ GB2312", 15F, System. drawing. fontStyle. bold, System. drawing. graphicsUnit. point, (System. byte) (134 )));
This. label4.forecolor = system. drawing. color. fromargb (system. byte) (255), (system. byte) (128), (system. byte) (128 )));
This. label4.location = new system. Drawing. Point (49, 16 );
This. label4.name = "label4 ";
This. label4.size = new system. Drawing. Size (245, 26 );
This. label4.tabindex = 7;
This. label4.text = "get the IP address and MAC address of the Local Computer ";
//
// GroupBox1
//
This. groupBox1.Controls. Add (this. button2 );
This.groupBox1.Controls.Add(this.txt Mac );
This.groupBox1.Controls.Add(this.txt Ip );
This.groupBox1.Controls.Add(this.txt Name );
This. groupBox1.Controls. Add (this. lblMac );
This. groupBox1.Controls. Add (this. lblIp );
This. groupBox1.Controls. Add (this. lblName );
This. groupBox1.Controls. Add (this. button1 );
This. groupBox1.Location = new System. Drawing. Point (16, 56 );
This. groupBox1.Name = "groupBox1 ";
This. groupBox1.Size = new System. Drawing. Size (320,208 );
This. groupBox1.TabIndex = 11;
This. groupBox1.TabStop = false;
This. groupBox1.Text = "Get zone ";
//
// Button2
//
This. button2.Font = new System. drawing. font ("", 9F, System. drawing. fontStyle. bold, System. drawing. graphicsUnit. point, (System. byte) (134 )));
This. button2.Location = new System. Drawing. Point (184,160 );
This. button2.Name = "button2 ";
This. button2.size = new system. Drawing. Size (88, 32 );
This. button2.tabindex = 18;
This. button2.text = "quit ";
This. button2.click + = new system. eventhandler (this. button2_click );
//
// Txtmac
//
This.txt Mac. backColor = System. drawing. color. fromArgb (System. byte) (224), (System. byte) (224), (System. byte) (224 )));
This.txt Mac. font = new System. drawing. font ("", 9F, System. drawing. fontStyle. bold, System. drawing. graphicsUnit. point, (System. byte) (134 )));
This.txt Mac. foreColor = System. drawing. color. fromArgb (System. byte) (255), (System. byte) (128), (System. byte) (0 )));
This.txt Mac. Location = new System. Drawing. Point (128,110 );
This.txt Mac. Name = "txtMac ";
This.txt Mac. ReadOnly = true;
This.txt Mac. Size = new System. Drawing. Size (168, 21 );
This.txt Mac. TabIndex = 17;
This.txt Mac. Text = "";
//
// TxtIp
//
This.txt Ip. backColor = System. drawing. color. fromArgb (System. byte) (224), (System. byte) (224), (System. byte) (224 )));
This.txt Ip. font = new System. drawing. font ("", 9F, System. drawing. fontStyle. bold, System. drawing. graphicsUnit. point, (System. byte) (134 )));
This.txt Ip. foreColor = System. drawing. color. fromArgb (System. byte) (255), (System. byte) (128), (System. byte) (0 )));
This.txt Ip. Location = new System. Drawing. Point (128, 70 );
This.txt Ip. Name = "txtIp ";
This.txt Ip. ReadOnly = true;
This.txt Ip. Size = new System. Drawing. Size (168, 21 );
This.txt IP. tabindex = 16;
This.txt IP. Text = "";
//
// Txtname
//
This.txt name. backcolor = system. drawing. color. fromargb (system. byte) (224), (system. byte) (224), (system. byte) (224 )));
This.txt name. font = new system. drawing. font ("", 9f, system. drawing. fontstyle. bold, system. drawing. graphicsunit. point, (system. byte) (134 )));
This.txt name. forecolor = system. drawing. color. fromargb (system. byte) (255), (system. byte) (128), (system. byte) (0 )));
This.txt name. Location = new system. Drawing. Point (128, 30 );
This.txt name. Name = "txtname ";
This.txt name. readonly = true;
This.txt name. size = new system. Drawing. Size (168, 21 );
This.txt name. tabindex = 15;
This.txt name. Text = "";
//
// Lblmac
//
This. lblmac. autosize = true;
This. lblmac. font = new system. drawing. font ("", 12f, system. drawing. fontstyle. bold, system. drawing. graphicsunit. point, (system. byte) (134 )));
This. lblmac. forecolor = system. drawing. color. fromargb (system. byte) (0), (system. byte) (0), (system. byte) (64 )));
This. lblmac. Location = new system. Drawing. Point (32,112 );
This. lblMac. Name = "lblMac ";
This. lblMac. Size = new System. Drawing. Size (88, 22 );
This. lblMac. TabIndex = 14;
This. lblMac. Text = "MAC address :";
//
// LblIp
//
This. lblIp. AutoSize = true;
This. lblip. font = new system. drawing. font ("", 12f, system. drawing. fontstyle. bold, system. drawing. graphicsunit. point, (system. byte) (134 )));
This. lblip. forecolor = system. drawing. color. fromargb (system. byte) (0), (system. byte) (0), (system. byte) (64 )));
This. lblip. Location = new system. Drawing. Point (40, 72 );
This. lblip. Name = "lblip ";
This. lblip. size = new system. Drawing. Size (80, 22 );
This. lblip. tabindex = 13;
This. lblIp. Text = "IP Address :";
//
// LblName
//
This. lblName. AutoSize = true;
This. lblName. font = new System. drawing. font ("", 12F, System. drawing. fontStyle. bold, System. drawing. graphicsUnit. point, (System. byte) (134 )));
This. lblName. foreColor = System. drawing. color. fromArgb (System. byte) (0), (System. byte) (0), (System. byte) (64 )));
This. lblname. Location = new system. Drawing. Point (24, 32 );
This. lblname. Name = "lblname ";
This. lblname. size = new system. Drawing. Size (97, 22 );
This. lblname. tabindex = 12;
This. lblname. Text = "computer name :";
//
// Button1
//
This. button1.Font = new System. drawing. font ("", 9F, System. drawing. fontStyle. bold, System. drawing. graphicsUnit. point, (System. byte) (134 )));
This. button1.Location = new System. Drawing. Point (48,160 );
This. button1.Name = "button1 ";
This. button1.Size = new System. Drawing. Size (88, 32 );
This. button1.TabIndex = 11;
This. button1.Text = "click to get ";
This. button1.Click + = new System. EventHandler (this. button#click_1 );
//
// Form1
//
This. AutoScaleBaseSize = new System. Drawing. Size (6, 14 );
This. ClientSize = new System. Drawing. Size (352,285 );
This. Controls. Add (this. groupBox1 );
This. Controls. Add (this. label4 );
This. FormBorderStyle = System. Windows. Forms. FormBorderStyle. Fixed3D;
This. MaximizeBox = false;
This. Name = "Form1 ";
This. StartPosition = System. Windows. Forms. FormStartPosition. CenterScreen;
This. Text = "IP, computer name, MAC query ";
This. groupBox1.ResumeLayout (false );
This. ResumeLayout (false );

}
# Endregion

/// <Summary>
/// Main entry point of the application.
/// </Summary>
[STAThread]
Static void Main ()
{
Application. Run (new Form1 ());
}

Private void button1_Click_1 (object sender, System. EventArgs e)
{
String s = "", mac = "";
//
// Name
//
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 ();
}
//
// 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 ();
}


TxtName. Text = hostInfo;
TxtIp. Text = s;
TxtMac. Text = mac;


Button1.Enabled = false;
Button2.Focus ();

}

Private void button2_Click (object sender, System. EventArgs e)
{
Application. Exit ();
}
}
}
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.