Get computer name and IP address with Visual C #

xiaoxiao2021-03-06  108

Visual C # is a next-generation program development language in Microsoft, is an important part of the Microsoft .NET framework. In the process of launching Visual C #, Microsoft has also launched a software development package corresponding to it - .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 the local LAN IP address Addr = new system.net.ipaddress (DNS.GETHOSTNAME (DNS.GETHOSTNAME ()) .addresslist [0 ] .Address); return addr.tostring ();} IV. Read computer name This machine fixed IP address source program IP01.CS source: // Import the namespace used by the program Using system; useing system.net; using System.Windows.Forms; Using System.drawing; Public Class Form3: form {// Defines two tags Private label label2; public static void main () {Application.Run (New Form3 ());} / / Constructed Form PUBLIC FORM3 () {// Setting Tags and initializes this.label1 = new system.windows.forms.label (); this.label2 = new system.windows.forms.label (); // Inherited Label class label1.location = new system.drawing.point (24, 16); label2.location = new system.drawing.point (44, 36); // Set the Label's display position label1.text = "host name: " System.net.dns.gethostname (); // Displays the computer name label2.text =" IP address: getipaddress (); // Displays the local area network IP address label1.size = new system. Drawing.size (200, 50); label2.size = new System.Drawing.Size (200, 80); // set the size of the labels label1.TabIndex = 0; label2.TabIndex = 1; label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; label2.TextAlign = System.Drawing. ContentAlignment.middleCenter; // Setting the label alignment this.text = "Get the 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 ("Song"

, 10, System.drawing.FontStyle.Bold); // Set the font, the size of the font THISSIZEGRIPSTY = System.Windows.Forms.SizeGripStyle.hide; this.clientsize = new system.drawing.size (250, 250); // Put the label to the form.controls.add (this.label1); this.controls.add (this.label2);} private static string getipaddress () {system.net.ipaddress Addr; / / Get the local LAN IP address addr = new system.net.ipaddress (DNS.GETHOSTBYNAME (DNS.GETHOSTNAME ()) .addressList [0] .address; return addr.tostring ();}} Compile with the following compile command After, csc /r :system.dll /r :system.windows.drawing.dll /r :system.drawing.dll /t:winexeip01.exe file, this file can read local fixed IP addresses . 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 has already said, the return value of the gethostbyname () method is the iphostiff object when IPHOSTENTRY object The properties of this object addressList, an array of ipaddress types that contain all IP address information at this 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: // Import the namespace used by the program Using System; use system.Net; use 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 ());} // Construction Form PUBLIC FORM3 ( ) {// establish a label and initialize this.label1 = new system.windows.forms.label (); this.label2 = new system.windows.forms.Label (); // 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-up dynamic allocation IP address label1.size = new system.drawing.size (200 , 50); label2.size = new system.drawing.size (200, 80); // Setting the size of the label Label1.TabINDEX = 0; label2.tabindex = 1; label1.textalign = system.drawing.contentalignment.MiddleCenter ; label2.textalign = system.drawing.contentalignment.Middlecent ER; / / Setting the label Align THISTEXT = "Get the 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 ("Song", 10, system.drawing.fontstyle.bold); // Set the font, size The style of the font this.sizegripStyle = system.windows.forms.sizeGripStyle.hide; this.clientsize = new system.drawing.size (250, 250); // Put the label to the form. THISTROLS.Add (this . label1);

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

New Post(0)