Get all IP addresses of the machine:
These addresses are ipv4 and ipv6 addresses containing all network cards (virtual network cards).
string name = (); IPAddress[] ipadrlist = (name);
Get all IPV4 addresses of the machine:
string name = (); IPAddress[] ipadrlist = (name); foreach (IPAddress ipa in ipadrlist) { if ( == ) (()); }
To obtain the IPv4 address simply, you can use the attribute to judge: for IPv4, return to InterNetwork; for IPv6, return to InterNetworkV6.
However, if the machine may have multiple IPv4 addresses, how to obtain the network card IP used when accessing the default gateway. I found a master's method in the CSDN forum, which is to query the native routing table.
Get the IPv4 address that the machine is using (IP that accesses the Internet)
Don't underestimate it, there are still many things to consider:
1. A computer has multiple network cards, two network cards with wired, wireless, and vmare virtual.
2. Even if there is only one network card, the network card is equipped with N IP addresses, which also includes the IPv6 address.
/// <summary> /// Get the currently used IP /// </summary> /// <returns></returns> public static string GetLocalIP() { string result = RunApp("route", "print",true); Match m = (result, @"0.0.0.0\s+0.0.0.0\s+(\d+.\d+.\d+.\d+)\s+(\d+.\d+.\d+.\d+)"); if () { return [2].Value; } else { try { c = new (); ("", 80); string ip = (()).(); (); return ip; } catch (Exception) { return null; } } } /// <summary> /// Get the native DNS /// </summary> /// <returns></returns> public static string GetPrimaryDNS() { string result = RunApp("nslookup", "",true); Match m = (result, @"\d+\.\d+\.\d+\.\d+"); if () { return ; } else { return null; } } /// <summary> /// Run a console program and return its output parameters. /// </summary> /// <param name="filename">Program name</param> /// <param name="arguments">Input parameters</param> /// <returns></returns> public static string RunApp(string filename, string arguments,bool recordLog) { try { if (recordLog) { (filename + " " + arguments); } Process proc = new Process(); = filename; = true; = arguments; = true; = false; (); using ( sr = new (, )) { //string txt = (); //(); //if (recordLog) //{ // (txt); //} //if (!) //{ // (); //} //The above is marked with the original text, and the following is modified by myself after debugging the error. (100); //It seems that the nslookup that calls the system has not returned the data or the data has not been encoded, and the program has skipped the execution directly //txt = (), resulting in the returned data being empty, so sleep causes the hardware to react if (!) //After calling nslookup without parameters, you can continue to enter the command to continue operations. If the process has not stopped, it will be executed directly { //txt = () The program is waiting for input, and it cannot be input, so it can be blocked directly and cannot continue running (); } string txt = (); (); if (recordLog) (txt); return txt; } } catch (Exception ex) { (ex); return ; } }
Another way to get it by using ipconfig:
private void GetIP() { Process cmd = new Process(); = "";//Set the program name = "/all"; //parameter //Redirecting standard output = true; = true; = false; = true;//The window is not displayed (the console program has a black screen) // = ;// I don't understand what it means for the time being /* Collect it, be prepared or not About: How to display it again after hiding? hwndWin32Host = (null, ); (hwndWin32Host, 1); //First find the window before ShowWindow */ (); string info = (); (); (); (info); }
The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!