C#获取IPConfig返回IP详细值
在我们获取本机局域网IP以及其他相关信息时,直接调用系统IPCONFIG,也是一种很有效的方法。
延伸说明:
我们调用IPCONFIG,其实和在运行里面输入IPCONFIG得到的结果是一样的。
既然这样我们就可以延伸的去调用其他的应用程序,并可获得调用的应用程序的输出。
读取IPConfig的返回值的代码:
/// <summary>
/// 获取IPCONFIG返回值
/// </summary>
/// <returns>返回 IPCONFIG输出</returns>
public static string GetIPConfigReturns()
{
string version = System.Environment.OSVersion.VersionString;
if (version.Contains("Windows"))
{
//调用ipconfig ,并传入参数: /all
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("ipconfig", "/all");
psi.CreateNoWindow = true; //若为false,则会出现cmd的黑窗体
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
System.Diagnostics.Process p = System.Diagnostics.Process.Start(psi);
return p.StandardOutput.ReadToEnd();
}
return string.Empty;
}
返回结果如下:
/*
Windows IP Configuration
Host Name . . . . . . . . . . . . : server
Primary Dns Suffix . . . . . . . :
Node Type . . . . . . . . . . . . : Unknown
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No
Ethernet adapter 本地连接:
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : NVIDIA nForce 10/100 Mbps Ethernet
Physical Address. . . . . . . . . : 00-E0-4C-BB-4F-AE
DHCP Enabled. . . . . . . . . . . : No
IP Address. . . . . . . . . . . . : 192.168.1.26
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.1.1
DNS Servers . . . . . . . . . . . : 268.10.24.56
202.103.44.150*/
原文链接:C#获取IPConfig返回IP详细值