Get computer name and IP address in Visual C #

Source: Internet
Author: User
Tags define array contains execution interface net string tostring
IP Address |visual Visual C # is the next generation of Microsoft's program development language, is an important part of the Microsoft. NET Framework, in the introduction of Visual C #, Microsoft also launched a corresponding software development package--. NET Framework Sdk. This software development package contains a number of classes, objects. Visual C # is the invocation of these classes, objects to achieve many of the more powerful features.
There are two namespaces available for network programming in the. Net FrameWork SDK, one is system.net and the other is system..net.socket. This article uses the classes and objects encapsulated in the first namespace to read the local computer name and all the IP addresses in the machine.

I. Overview:
We know that for a computer, he has only one computer name, but he can have more than one IP address. For example, when the computer through dial-up Internet, after verifying the user name and password, will dynamically assign an IP address, at this time the computer has two IP addresses, one set their own LAN IP address, the other is a dial-up Internet dynamic allocation of IP address. This article is to explore how to read these two IP addresses and computer names.

Two. Design and operation of the environment:
(1) Microsoft company Windows 2000 Server Edition
(2). Net Framewrok SDK Beta 2 Edition

Three. The main ideas and implementation methods of program design:
(1). Read the name of the computer:
A class DNS is defined in namespace System.Net, in which a more important method gethostname () is defined, and the return value of this method is the local computer name. In programming, you first import the System.Net namespace and then read the local computer name by calling the GetHostName () method in the DNS class, which is the main statement of the implementation:
Label1. Text = "Host name:" + System.Net.Dns.GetHostName ();
(2). Read the computer's dial-up Internet temporary IP address and the fixed IP address of the local area network allocation:
In programming, we read the IP address through a custom function--getipaddress (). First look at how to read a locally fixed IP address. A method gethostbyname () is also defined in the DNS class. The return value of this method iphostentry the object, which has a property that is AddressList, an array of IPAddress types that contains all the IP address information for the computer at this time. This also includes the dial-up Internet access to the temporary allocation of IP address and LAN fixed IP address. The specific implementation statement is as follows:
private static string Getipaddress ()
{
System.Net.IPAddress addr;
Get native LAN IP address
addr = new System.Net.IPAddress (Dns.gethostbyname (Dns.gethostname ()). AddressList [0]. address);
Return addr. ToString ();
}

Four. Read computer name native fixed IP address source program
IP01.cs source program:
Namespaces used by the importer
Using System;
Using System.Net;
Using System.Windows.Forms;
Using System.Drawing;
public class Form3:form
{
Define two labels
Private Label Label1;
Private Label Label2;
public static void Main ()
{
Application.Run (New FORM3 ());
}
Constructing a form
Public Form3 ()
{
Creates a label and initializes the
This.label1 = new System.Windows.Forms.Label ();
This.label2 = new System.Windows.Forms.Label ();
Inherit a label class first
Label1. Location = new System.Drawing.Point (24, 16);
Label2. Location = new System.Drawing.Point (44, 36);
Set the display position of the label
Label1. Text = "Host name:" + System.Net.Dns.GetHostName ();
Displays the computer name of this machine
Label2. Text = "IP Address:" + getipaddress ();
Displays the local area network IP address of the computer
Label1. Size = new System.Drawing.Size (200, 50);
Label2. Size = new System.Drawing.Size (200, 80);
Set the size of the label
Label1. TabIndex = 0;
Label2. TabIndex = 1;
Label1. TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
Label2. TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
To set the alignment of a label
This. Text = "Get host name and IP address!" " ;
This. StartPosition = System.Windows.Forms.FormStartPosition.CentERParent;
This. AutoScaleBaseSize = new System.Drawing.Size (8, 16);
This. FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
Set the boundary type of a form
This. ForeColor = System.Drawing.SystemColors.Desktop;
This. Font = new System.Drawing.Font ("XXFarEastFont-Arial", System.Drawing.FontStyle.Bold);
Set the font, size, font style
This. Sizegripstyle = System.Windows.Forms.SizeGripStyle.Hide;
This. ClientSize = new System.Drawing.Size (250, 250);
Add the label to the form
This. Controls.Add (THIS.LABEL1);
This. Controls.Add (THIS.LABEL2);
}
private static string Getipaddress ()
{
System.Net.IPAddress addr;
Get native LAN IP address
addr = new System.Net.IPAddress (Dns.gethostbyname (Dns.gethostname ()). AddressList [0]. address);
Return addr. ToString ();
}
}


After compiling with the following compilation command,
Csc/r:system.dll/r:system.windows.forms.dll/r:system.drawing.dll/t:winexeip01.cs
Get the Ip01.exe file, this file can read the local fixed IP address. The following is the interface after execution:

Figure 01: Reading the computer name and fixed IP address

Five. Read computer name and dial-up Internet dynamically allocated IP address source program
As already mentioned, the return value of the gethostbyname () method Iphostentry the object, and the object's property AddressList is an array of IPAddress types that contains all the IP address information at this time on the computer. In the Ip01.cs addresslist [0]. Address is a fixed IP, and the IP address that is dynamically assigned to the Internet is. AddressList [1]. Address. Accordingly we can get read dial-up Internet dynamically assigned IP address source program:
IP02.cs source program:
Namespaces used by the importer
Using System;
Using System.Net;
Using System.Windows.Forms;
Using System.Drawing;
public class Form3:form
{
Define two labels
Private Label Label1;
Private Label Label2;
public static void Main ()
{
Application.Run (New FORM3 ());
}
Constructing a form
Public Form3 ()
{
Creates a label and initializes the
This.label1 = new System.Windows.Forms.Label ();
This.label2 = new System.Windows.Forms.Label ();
Inherit a label class first
Label1. Location = new System.Drawing.Point (24, 16);
Label2. Location = new System.Drawing.Point (44, 36);
Set the display position of the label
Label1. Text = "Host name:" + System.Net.Dns.GetHostName ();
Displays the computer name of this machine
Label2. Text = "IP Address:" + getipaddress ();
Displays the dial-up dynamic allocation IP address for this computer
Label1. Size = new System.Drawing.Size (200, 50);
Label2. Size = new System.Drawing.Size (200, 80);
Set the size of the label
Label1. TabIndex = 0;
Label2. TabIndex = 1;
Label1. TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
Label2. TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
To set the alignment of a label
This. Text = "Get host name and IP address!" " ;
This. StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
This. AutoScaleBaseSize = new System.Drawing.Size (8, 16);
This. FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
Set the boundary type of a form
This. ForeColor = System.Drawing.SystemColors.Desktop;
This. Font = new System.Drawing.Font ("XXFarEastFont-Arial", System.Drawing.FontStyle.Bold);
Set the font, size, font style
This. Sizegripstyle = System.Windows.Forms.SizeGripStyle.Hide;
This. ClientSize = new System.Drawing.Size (250, 250);
Add the label to the form
This. Controls.Add (THIS.LABEL1);
This. Controls.Add (THIS.LABEL2);
}
private static string Getipaddress ()
{
System.Net.IPAddress addr;
Get dial-up dynamic Assignment IP address
addr = new System.Net.IPAddress (Dns.gethostbyname (Dns.gethostname ()). AddressList [1]. address);
Return addr. ToString ();
}
}

Once the compilation is complete, execution can be done with the following runtime interface:

Figure 02: Reading the computer name and dynamic IP address

Six. Summary:
This article is through two examples to read the machine's computer name and different IP address, through the above two examples, we can see if the machine has three or more IP address, we can also set addresslist different values to get the machine different IP address.
The namespace System.Net also provides a number of network-oriented programming classes that are so powerful that they can be used flexibly to develop many powerful Web applications.

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.