Using system;using system.collections.generic;using system.collections.specialized;using System.ComponentModel; Using system.data;using system.drawing;using system.linq;using system.net;using system.net.sockets;using System.Text ; using system.threading.tasks;using system.windows.forms;namespace getipv4application{public partial class Form1:for m {public Form1 () {InitializeComponent (); } private void Btn_getipv4_click (object sender, EventArgs e) {Showip (); } void Showip () {Richtextboxipv4.text = string. Empty; The IPv4 address may also be more than one foreach (String IP in GetLocalIpv4 ()) {This.richTextBoxIPv4.Append Text (IP. ToString () + "\ n"); } return; } string[] GetLocalIpv4 () {//Do not know the number of IP in advance, the array length is unknown, so use StringCollection to store ipaddress[] Lo Calips; Localips = Dns.gethostaddresses (Dns.gethostname ()); StringCollection ipcollection = new StringCollection (); foreach (IPAddress IP in localips) {//Based on addressfamily to determine if IPv4, if InterNetWorkV6 is IPv6 if (IP. AddressFamily = = addressfamily.internetwork) ipcollection.add (IP. ToString ()); } string[] Iparray = new String[ipcollection.count]; Ipcollection.copyto (Iparray, 0); return iparray; } }}
Note: Multiple IP addresses are obtained when a virtual network card is available.
C # get IPV4 code