獲取本地ip地址 C#


與ipconfig獲取的所有信息一致的方法:

private void GetIp()
        {
            System.Diagnostics.Process cmdp= new System.Diagnostics.Process();
            cmdp.StartInfo.FileName = "ipconfig.exe";//設置程序名     
            cmdp.StartInfo.Arguments = "/all";  //參數     
            //重定向標准輸出     
            cmdp.StartInfo.RedirectStandardOutput = true;
            cmdp.StartInfo.RedirectStandardInput = true;
            cmdp.StartInfo.UseShellExecute = false;
            cmdp.StartInfo.CreateNoWindow = true;//不顯示窗口---控制台程序是黑屏
            //cmdp.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//暫時不明白什么意思     
            /*  
     收集一下,有備無患  
            關於:ProcessWindowStyle.Hidden 隱藏后如何再顯示?  
            hwndWin32Host = Win32Native.FindWindow(null, win32Exinfo.windowsName);  
            Win32Native.ShowWindow(hwndWin32Host, 1);     //先FindWindow 找到窗口后再ShowWindow  
            */
            cmd.Start();
            string info = cmdp.StandardOutput.ReadToEnd();
            cmdp.WaitForExit();
            cmdp.Close();
            
        }
View Code

 

單獨獲取本地ip地址出來的方法:

/// <summary>  
        /// 獲取當前使用的ip  
        /// </summary>  
        /// <returns></returns>  
        public static string GetLocalIp()
        {
            string result = RunApp("route", "print", true);
            System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(result, @"0.0.0.0\s+0.0.0.0\s+(\d+.\d+.\d+.\d+)\s+(\d+.\d+.\d+.\d+)");
            if (m.Success)
            {
                return m.Groups[2].Value;
            }
            else
            {
                try
                {
                    System.Net.Sockets.TcpClient t = new System.Net.Sockets.TcpClient();
                    t.Connect("www.baidu.com", 80);
                    string ip = ((System.Net.IPEndPoint)t.Client.LocalEndPoint).Address.ToString();
                    t.Close();
                    return ip;
                }
                catch (Exception)
                {
                    return null;
                }
            }
        }

        /// <summary>  
        /// 獲取本機主DNS  
        /// </summary>  
        /// <returns></returns>  
        public static string GetPrimaryDNS()
        {
            string result = RunApp("nslookup", "", true);
            System.Text.RegularExpressions.Match mat = System.Text.RegularExpressions.Regex.Match(result, @"\d+\.\d+\.\d+\.\d+");
            if (mat.Success)
            {
                return mat.Value;
            }
            else
            {
                return null;
            }
        }

        /// <summary>  
        /// 運行一個控制台程序並返回其輸出參數。  
        /// </summary>  
        /// <param name="filename">程序名</param>  
        /// <param name="arguments">輸入參數</param>  
        /// <returns></returns>  
        public static string RunApp(string filename, string arguments, bool recordLog)
        {
            try
            {
                if (recordLog)
                {
                    System.Diagnostics.Trace.WriteLine(filename + " " + arguments);
                }
                System.Diagnostics.Process procezz = new System.Diagnostics.Process();
                procezz.StartInfo.FileName = filename;
                procezz.StartInfo.CreateNoWindow = true;
                procezz.StartInfo.Arguments = arguments;
                procezz.StartInfo.RedirectStandardOutput = true;
                procezz.StartInfo.UseShellExecute = false;
                procezz.Start();

                using (System.IO.StreamReader sr = new System.IO.StreamReader(procezz.StandardOutput.BaseStream, Encoding.Default))
                {
                    //string txt = sr.ReadToEnd();  
                    //sr.Close();  
                    //if (recordLog)  
                    //{  
                    //    Trace.WriteLine(txt);  
                    //}  
                    //if (!proc.HasExited)  
                    //{  
                    //    proc.Kill();  
                    //}  
                    //上面標記的是原文,下面是我自己調試錯誤后自行修改的  
                    System.Threading.Thread.Sleep(100);           //貌似調用系統的nslookup還未返回數據或者數據未編碼完成 程序就已經跳過直接執行  
                    //txt = sr.ReadToEnd()了,導致返回的數據為空 故睡眠令硬件反應  
                    if (!procezz.HasExited)         //在無參數調用nslookup后 可以繼續輸入命令繼續操作 如果進程未停止就直接執行  
                    {                            //txt = sr.ReadToEnd()程序就在等待輸入 而且又無法輸入 直接掐住無法繼續運行  
                        procezz.Kill();
                    }
                    string txt = sr.ReadToEnd();
                    sr.Close();
                    if (recordLog)
                        System.Diagnostics.Trace.WriteLine(txt);
                    return txt;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex);
                return ex.Message;
            }
        }
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM