C#獲取IPConfig返回IP詳細(xì)值

在我們獲取本機(jī)局域網(wǎng)IP以及其他相關(guān)信息時(shí),直接調(diào)用系統(tǒng)IPCONFIG,也是一種很有效的方法。

延伸說明:
我們調(diào)用IPCONFIG,其實(shí)和在運(yùn)行里面輸入IPCONFIG得到的結(jié)果是一樣的。
既然這樣我們就可以延伸的去調(diào)用其他的應(yīng)用程序,并可獲得調(diào)用的應(yīng)用程序的輸出。

讀取IPConfig的返回值的代碼:


         /// <summary>
        /// 獲取IPCONFIG返回值
        /// </summary>
        /// <returns>返回 IPCONFIG輸出</returns>
        public static string GetIPConfigReturns()
        {
            string version = System.Environment.OSVersion.VersionString;

            if (version.Contains("Windows"))
            {
                //調(diào)用ipconfig ,并傳入?yún)?shù): /all
                System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("ipconfig", "/all");

                psi.CreateNoWindow = true; //若為false,則會(huì)出現(xiàn)cmd的黑窗體
                psi.RedirectStandardOutput = true;
                psi.UseShellExecute = false;

                System.Diagnostics.Process p = System.Diagnostics.Process.Start(psi);

                return p.StandardOutput.ReadToEnd();
            }

            return string.Empty;
        }
返回結(jié)果如下:



/*
        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詳細(xì)值