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一样的效果。既然这样我们就可以延伸的去调用其他的 应用程序,并可获得调用的应用程序的输出。

 

 【引用请声明文章出处】

posted on 2010-08-07 10:21  VicentRen  阅读(2867)  评论(0编辑  收藏  举报

清醒清醒头脑,弄明白今天该做些什么,做了些什么...