C# 获取IPCONFIG 返回值

在我们获取本机局域网IP以及其他相关信息时,直接调用系统IPCONFIG,也是一种很有效的方法。

以下是我用C#实现的 读取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 . . . . . . . . . . . : 202.103.24.68

                                               202.103.44.150
          
*/

扩展说明:

这里我们调用的是IPCONFIG,其实就是想在运行里面输入IPCONFIG一样的效果。既然这样我们就可以延伸的去调用其他的 应用程序,并可获得调用的应用程序的输出。

原文地址:https://www.cnblogs.com/hantianwei/p/1674430.html