zoukankan      html  css  js  c++  java
  • 获取本地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.0s+0.0.0.0s+(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
  • 相关阅读:
    使用beautifulsoup4解析内容
    爬虫的概述以及使用(request发送请求)
    爬虫的概述以及使用(urllib发送请求)
    NoteBook的安装和使用
    云端服务器
    SQLServer数据库访问链接
    python爬虫调度器
    Python的应用领域
    Python小记2
    Python小记1
  • 原文地址:https://www.cnblogs.com/bit-by-bit/p/7677167.html
Copyright © 2011-2022 走看看