天天看點

C# 擷取IPCONFIG 傳回值

在我們擷取本機區域網路IP以及其他相關資訊時,直接調用系統IPCONFIG,也是一種很有效的方法。

以下是我用C#實作的 讀取ipconfig的傳回值的代碼:

C# 擷取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;

        }

以下是傳回的結果:

C# 擷取IPCONFIG 傳回值
C# 擷取IPCONFIG 傳回值

/*傳回結果

        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一樣的效果。既然這樣我們就可以延伸的去調用其他的 應用程式,并可獲得調用的應用程式的輸出。

繼續閱讀