//1.Dns.GetHostName方法获取IPv4
using System.Net;
string hostName = Dns.GetHostName();
IPAddress[] addressList = Dns.GetHostAddresses(hostName);
foreach (IPAddress ip in addressList)
{
Console.WriteLine(ip.ToString());
}
//2.使用Process类调用ipconfig获取
using System.Diagnostics;
Process cmd = new Process();
cmd.StartInfo.FileName = "ipconfig.exe";
cmd.StartInfo.Arguments = "/all";
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.CreateNoWindow = true;
cmd.Start();
string info = cmd.StandardOutput.ReadToEnd();
cmd.Close();
Console.WriteLine(info);