Get computer name and IP address with Visual C #

xiaoxiao2021-03-06  39

With Visual C # get a computer name and IP address Visual C # is a next-generation program development language launched by Microsoft. It is an important part of the Microsoft .NET framework. In the process of launching Visual C #, Microsoft has also launched it. A software development package - .NET Framework SDK. There are many classes, objects in this software development package. Visual C # is to achieve many more powerful functions by calling these classes. Two namespaces available for network programming in the .NET Framework SDK, one is system.net, the other is system..Net.socket. This article is to read the local computer name and all IP addresses in the machine in the first namespace. I. Overview: We know that he has only one computer name for a computer, but he can have multiple IP addresses. For example, when the computer passes the network, after verifying the username and password, it will dynamically assign an IP address. At this time, the computer has two IP addresses, one of the IP addresses used by the local area network, and another It is the IP address that dials the dynamic allocation of the Internet. This article is to explore how to read this two IP addresses and computer names. II. Environment for programming and operation: (1) Microsoft Window 2000 Server Edition (2) .NET Framewrok SDK Beta 2 Edition 3. Main ideas and implementation methods: (1). Name of the computer: A class DNS defines a class DNS in the namespace system.net, defining a more important way gethostname (), and the return value of this method is the local computer name. In the program design, you must first import the System.net namespace, and then read the local computer name by calling the gethostname () method in the DNS class. The main statement of the specific implementation is as follows: label1.text = "host name:" system.net .Dns.gethostname (); (2). Read the computer's dial-up network temporary IP address and fixed IP address of the LAN assignment: In the programming, we read through a custom function - GetipAddress () IP address. First look at how to read local fixed IP addresses. A method GethostByname () is also defined in the DNS class. When the return value of this method, the IPHOSTENTRY object is an attribute that is addressList, which is an array of ipaddress types that contain all IP address information at this time. This is also a temporary IP address of the dial-up Internet access and a fixed IP address of the LAN. The specific implementation statement is as follows:

Private static string getipaddress () {system.net.Ipaddress addr; // Get native local area network IP address addr = new system.net.ipaddress (DNS.GETHOSTBYNAME (DNS.GETHOSTNAME ()) .addresslist [0] .address); Return addr.tostring ();} IV. Read the computer name This machine fixed IP address source program IP01.CS source program:

// Import the name of the name for use in the name, use system.net; use system.windows.form; use system.drawing; public class form3: form {// Define two tags Private label label1; private label label2; public Static void main () {Application.Run (New Form3 ());} // Construct Forms Public Form3 () {// Setting Tags and initializes this.label1 = new system.windows.forms.label (); this. Label2 = new system.windows.forms.label (); // first inherited a label class label1.location = new system.drawing.point (24, 16); label2.location = new system.drawing.point (44, 36 ); // set Label's display position label1.text = "host name:" system.net.dns.getHostName (); // Displays the computer name 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); // Setting the label size label1.TabIndex = 0; label2.TabIndex = 1; label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // set the alignment tab this.Text = "obtained 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 form boundary types this.ForeColor = System.drawing.systemcolors.desktop; this.font = new system.drawing.font ("Song", 10, System.drawing.FontStyle.bold); // Set the font, size THISSIZEGRIPSTYLE = SYSTEM .Windows.forms.sizegripStyle.hide; this.clientsize = new system.drawing.size (250, 250); // Put the label to the form. THISCONTROLS.Add (this.label1); this.controls.add (this.label2);

// Get the local LAN IP address addr = new system.net.Ipaddress (DNS.GETHOSTBYNAME (DNS.GETHOSTNAME ()) .addresslist [0] .address; return addr.tostring ();}} After the following compile command After compiling, csc /r:system.dll /r:system.windows.drawing.dll /r:System.drawing.dll /t:winexeip01.cs gets an IP01.exe file, this file can read 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 dial-up dynamic allocation IP address source that has already said, the return value of the gethostbyname () method, the IPHOSTENTRY object, this object's properties addressList, an array of iPadDress types, including the computer All IP address information of time. In IP01.CS, addresslist [0] .Address is a fixed IP address, and the IP address that is dynamically allocated to the Internet is .addressList [1] .address. According to this, we can get the source program of the IP address that reads dial-up Internet dynamically assigned: IP02.cs source program:

// Import the name of the name for use in the name, use system.net; use system.windows.form; use system.drawing; public class form3: form {// Define two tags Private label label1; private label label2; public Static void main () {Application.Run (New Form3 ());} // Construct Forms Public Form3 () {// Setting Tags and initializes this.label1 = new system.windows.forms.label (); this. Label2 = new system.windows.forms.label (); // first inherited a label class label1.location = new system.drawing.point (24, 16); label2.location = new system.drawing.point (44, 36 ); // set Label's display position label1.text = "host name:" system.net.dns.getHostName (); // Displays the computer name label2.text = "IP address:" GetIpAddress ); // Display the dial dynamic allocation IP address label1.size = new system.drawing.size (200); label2.size = new system.drawing.size (200, 80); // Setting tag size label1.TabIndex = 0; label2.TabIndex = 1; label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // set the alignment tab this.Text = "Get the host name and IP address!" This.StartPosition = system.wind ows.Forms.FormStartPosition.CenterParent; this.AutoScaleBaseSize = new System.Drawing.Size (8, 16); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D; // set form boundary types this.ForeColor = System.drawing.systemcolors.desktop; this.font = new system.drawing.font ("Song", 10, System.drawing.FontStyle.bold); // Set the font, size THISSISEGRIPSTYLE = System.Windows.Forms.SizeGripStyle.hide; this.clientsize = new system.drawing.size (250, 250); // Put the label to the form. THISCONTROLS.ADD (this.label1); this.Controls. ADD (this.label2);} private static string getipaddress () {system.net.ipaddress addr;

// Get dial-up dynamic allocation IP address addr = new system.net.Ipaddress (DNS.GethostName ()) .addresslist [1] .address; return addr.tostring ();}} After completion, execute You can get the following interface: Figure 02: Read the computer name and dynamic IP address

Search: This article reads out two examples to read the machine's computer name and different IP addresses. Through the above two examples, we can see that if the machine has three or more IP addresses, we can also set The different values ​​of AddressList are different from the IP address of the machine. In the namespace system.net, there are also many-to-network programming classes, which are very huge, flexible and flexible, can develop a lot of powerful web applications.

转载请注明原文地址:https://www.9cbs.com/read-71118.html

New Post(0)