• C#获取本机IP方法,获取本机局域网IP地址方法


    1.

     1 private void GetIP()  
     2 {  
     3     string hostName = Dns.GetHostName();//本机名   
     4     //System.Net.IPAddress[] addressList = Dns.GetHostByName(hostName).AddressList;//会警告GetHostByName()已过期,我运行时且只返回了一个IPv4的地址   
     5     System.Net.IPAddress[] addressList = Dns.GetHostAddresses(hostName);//会返回所有地址,包括IPv4和IPv6   
     6     foreach (IPAddress ip in addressList)  
     7     {  
     8         listBox1.Items.Add(ip.ToString());  
     9     }  
    10 }  

    2.使用IPHostEntry获取本机局域网地址

            public static string GetLocalIp()
            {
                //得到本机名 
                string hostname = Dns.GetHostName();
                //解析主机名称或IP地址的system.net.iphostentry实例。
                IPHostEntry localhost = Dns.GetHostEntry(hostname);
                if (localhost != null)
                {
                    foreach (IPAddress item in localhost.AddressList)
                    {
                        //判断是否是内网IPv4地址
                        if (item.AddressFamily== AddressFamily.InterNetwork)
                        {
                            return item.MapToIPv4().ToString();
                        }
                    }
                }
                return "192.168.1.124";
            }

    3.通过向网站向一些提供IP查询的网站发送webrequest,然后分析返回的数据流 

     1        string strUrl = "提供IP查询的网站的链接";  
     2        Uri uri = new Uri(strUrl);  
     3        WebRequest webreq = WebRequest.Create(uri);  
     4        Stream s = webreq .GetResponse().GetResponseStream();  
     5        StreamReader sr = new StreamReader(s, Encoding.Default);  
     6        string all = sr.ReadToEnd();   
     7        int i = all.IndexOf("[") + 1;  
     8        //分析字符串得到IP   
     9        return ip;  
    10        /* 
    11         我用的是http://www.ip.cn/getip.php?action=getip&ip_url=&from=web    
    12         (这种链接很容易找的,百度“IP”得到一些网站,分析一下网站的链接就能得到) 
    13         返回的数据是:  
    14         <div class="well"><p>当前 IP:<code>0.0.0.0</code>&nbsp;来自:XX省XX市 电信</p><p>GeoIP: Beijing, China</p></div>  
    15         解析这段就行  
    16       */  

    4.通过获取CMD里ipconfig命令的结果来得到IP

     1    private void GetIP6()  
     2    {  
     3        Process cmd = new Process();  
     4        cmd.StartInfo.FileName = "ipconfig.exe";//设置程序名   
     5        cmd.StartInfo.Arguments = "/all";  //参数   
     6 //重定向标准输出   
     7        cmd.StartInfo.RedirectStandardOutput = true;  
     8        cmd.StartInfo.RedirectStandardInput = true;  
     9        cmd.StartInfo.UseShellExecute = false;  
    10        cmd.StartInfo.CreateNoWindow = true;//不显示窗口(控制台程序是黑屏)   
    11 //cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//暂时不明白什么意思   
    12        /* 
    13 收集一下 有备无患 
    14        关于:ProcessWindowStyle.Hidden隐藏后如何再显示? 
    15        hwndWin32Host = Win32Native.FindWindow(null, win32Exinfo.windowsName); 
    16        Win32Native.ShowWindow(hwndWin32Host, 1);     //先FindWindow找到窗口后再ShowWindow 
    17        */  
    18        cmd.Start();  
    19        string info = cmd.StandardOutput.ReadToEnd();  
    20        cmd.WaitForExit();  
    21        cmd.Close();  
    22        textBox1.AppendText(info);  
    23    }  
  • 相关阅读:
    convert image to base64 and post to RESTful wcf
    在android webview实现截屏的手动tounchmove裁剪图片
    How to use jquery ajax and android request security RESTful WCF
    using swfUpload in asp.net mvc
    using HttpClient and sending json data to RESTful server in adroind
    ODP.NET数据访问
    android image watermark
    解决国内不能访问github的问题
    idapro权威指南第二版阅读笔记第九章 交叉引用和绘图功能
    idapro权威指南第二版阅读笔记第二章 逆向和反汇编工具
  • 原文地址:https://www.cnblogs.com/tianma3798/p/4197423.html
Copyright © 2020-2023  润新知