Use Visual C # To obtain the computer name and IP address

Source: Internet
Author: User
From http://www.ddvip.net/program/c-/index5/26.htm

Visual C # is the next-generation programming language launched by Microsoft. It is Microsoft. an important part of the. NET Framework. In the process of launching Visual C #, Microsoft also launched a software development kit --.. NET Framework SDK. This software development kit encapsulates many classes and objects. Visual C # implements many powerful functions by calling these classes and objects.
In the. NET Framework SDK, the zookeeper ystem. NET and system. net. socket are provided. This article uses the classes and objects encapsulated in the first namespace to read the name of the Local Computer and all the IP addresses of the machine.

I. Overview:
We know that a computer has only one computer name, but it can have multiple IP addresses. For example, when a computer attempts to access the Internet through dial-up, after verifying the user name and password, it will dynamically allocate an IP address. In this case, the computer has two IP addresses, one is the IP address used by the local area network and the other is the IP address dynamically allocated by the dial-up. This article describes how to read the two IP addresses and computer names.

II. Environment for program design and operation:
(1) Microsoft Windows 2000 Server Edition
(2). Net framewrok SDK beta 2

Iii. Main Ideas and implementation methods of Program Design:
(1). Read the computer name:
In the namespace system. Net, a DNS class is defined, and an important method gethostname () is defined in this class. The return value of this method is the name of the local computer. In programming, you must first import the system. Net namespace, and then call the gethostname () method in the DNS class to read the name of the local computer. The main statement for implementation is as follows:
Label1.text = "Host Name:" + system. net. DNS. gethostname ();
(2). Read the temporary IP address of the computer's dial-up Internet access and the fixed IP address assigned by the LAN:
In programming, we read the IP address through a custom function, getipaddress. First, let's take a look at how to read a local fixed IP address. A Method gethostbyname () is also defined in the DNS class (). When the return value of this method is iphostentry, this object has an attribute of Addresslist, which is an array of IPaddress type and contains all the IP address information of the computer at this time. This also includes the temporary allocated IP address obtained by dialing the Internet and the fixed IP address of the LAN. The specific implementation statement is as follows:

Private Static string getipaddress ()
{
System. net. IPaddress ADDR;
// Obtain the local lan ip Address
ADDR = new system. net. IPaddress (DNS. gethostbyname (DNS. gethostname (). Addresslist [0]. Address );
Return ADDR. tostring ();
}

4. Read the computer name the source program of the local fixed IP Address
Ip01.cs source code:

// Namespace used by the import program
Using system;
Using system. net;
Using system. Windows. forms;
Using system. drawing;
Public class form3: Form
{
// Define two tags
Private Label label1;
Private Label label2;
Public static void main ()
{
Application. Run (New form3 ());
}
// Construct a form
Public form3 ()
{
// Create a tag and initialize it
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 Label display position
Label1.text = "Host Name:" + system. net. DNS. gethostname ();
// Display the computer name of the Local Machine
Label2.text = "IP Address:" + getipaddress ();
// Display the local area network IP address
Label1.size = new system. Drawing. Size (200, 50 );
Label2.size = new system. Drawing. Size (200, 80 );
// Set the label size
Label1.tabindex = 0;
Label2.tabindex = 1;
Label1.textalign = system. Drawing. contentalignment. middlecenter;
Label2.textalign = system. Drawing. contentalignment. middlecenter;
// Set the alignment of tags
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 the form
This. forecolor = system. Drawing. systemcolors. desktop;
This. font = new system. Drawing. Font ("", 10, system. Drawing. fontstyle. Bold );
// Set the font and font size.
This. sizegripstyle = system. Windows. Forms. sizegripstyle. Hide;
This. clientsize = new system. Drawing. Size (250,250 );
// Add the tag to the form
This. Controls. Add (this. label1 );
This. Controls. Add (this. label2 );
}
Private Static string getipaddress ()
{
System. net. IPaddress ADDR;
// Obtain the local lan ip Address
ADDR = new system. net. IPaddress (DNS. gethostbyname (DNS. gethostname (). Addresslist [0]. Address );
Return ADDR. tostring ();
}

}

After the following compilation commands are compiled,
CSC/R: system. dll/R: system. Windows. Forms. dll/R: system. Drawing. dll/T: winexeip01.cs
To get the ip01.exe file, this file can read the local fixed IP address. The following is the execution interface:

Figure 01: Read the computer name and fixed IP Address
5. Read the computer name and the IP address dynamically allocated by the dial-up. Source Code
As mentioned above, the iphostentry object is returned by the gethostbyname () method. The Addresslist attribute of this object is an array of the IPaddress type and contains all the IP address information of the computer at this time. In ip01.cs, Addresslist [0]. Address is a fixed IP address, and the IP address dynamically allocated on the internet is. Addresslist [1]. Address. Based on this, we can obtain the source code for reading the IP address dynamically allocated to the dial-up Internet:
Ip02.cs source code:

// Namespace used by the import program
Using system;
Using system. net;
Using system. Windows. forms;
Using system. drawing;
Public class form3: Form
{
// Define two tags
Private Label label1;
Private Label label2;
Public static void main ()
{
Application. Run (New form3 ());
}
// Construct a form
Public form3 ()
{
// Create a tag and initialize it
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 Label display position
Label1.text = "Host Name:" + system. net. DNS. gethostname ();
// Display the computer name of the Local Machine
Label2.text = "IP Address:" + getipaddress ();
// Display the dynamic IP address allocated for local dialing
Label1.size = new system. Drawing. Size (200, 50 );
Label2.size = new system. Drawing. Size (200, 80 );
// Set the label size
Label1.tabindex = 0;
Label2.tabindex = 1;
Label1.textalign = system. Drawing. contentalignment. middlecenter;
Label2.textalign = system. Drawing. contentalignment. middlecenter;
// Set the alignment of tags
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 the form
This. forecolor = system. Drawing. systemcolors. desktop;
This. font = new system. Drawing. Font ("", 10, system. Drawing. fontstyle. Bold );
// Set the font and font size.
This. sizegripstyle = system. Windows. Forms. sizegripstyle. Hide;
This. clientsize = new system. Drawing. Size (250,250 );
// Add the tag to the form
This. Controls. Add (this. label1 );
This. Controls. Add (this. label2 );
}
Private Static string getipaddress ()
{
System. net. IPaddress ADDR;
// Obtain the IP address dynamically allocated for dialing
ADDR = new system. net. IPaddress (DNS. gethostbyname (DNS. gethostname (). Addresslist [1]. Address );
Return ADDR. tostring ();
}
}

After the compilation is complete, run the following running interface:

Figure 02: Read the computer name and dynamic IP Address
6. Summary:
This article uses two examples to read the computer name and different IP addresses of the machine. With the two examples above, we can see that if the machine has three or more IP addresses, we can also set different values of Addresslist to get different IP addresses of machines.
In the namespace system. net also provides a lot of network-oriented programming classes. These classes have huge functions. They can be used flexibly to develop many powerful network applications.

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.