C#獲取外網IP、本機MAC地址及Ping的實現


原文 獲取外網IP, C#獲取本機的MAC地址C#通過編程方式實現Ping

獲取外網IP地址

思路是通過WebRequest連接一些網上提供IP查詢服務的網站,下載到含有你的IP的網頁,然后用正則表達式提取出IP來

class Program  
{  
    static void Main(string[] args)  
    {  
        Console.WriteLine(GetExportIP());  
  
        Console.ReadKey();  
    }  
  
    public static string GetExportIP()   
    {  
        //獲取外部IP  
        string strUrl = "http://www.ip.cn/getip.php?action=getip&ip_url=&from=web";  
        //string strUrl = "http://216.157.85.151/getip.php?action=getip&ip_url=&from=web";  
        Uri uri = new Uri(strUrl);  
        WebRequest webreq = WebRequest.Create(uri);  
        Stream s = webreq.GetResponse().GetResponseStream();  
        StreamReader sr = new StreamReader(s, Encoding.Default);  
        string all = sr.ReadToEnd();  
        all = Regex.Replace(all,@"(\d+)","000$1");  
        all = Regex.Replace(all, @"0+(\d{3})", "$1");  
        string reg = @"(\d{3}\.\d{3}\.\d{3}\.\d{3})";  
        Regex regex = new Regex(reg);  
        Match match = regex.Match(all);  
        string ip = match.Groups[1].Value;  
        return Regex.Replace(ip,@"0*(\d+)","$1");  
    }  
}  

 獲取本機MAC

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Management;  
  
namespace _17獲取MAC地址  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            ManagementObjectSearcher nisc = new ManagementObjectSearcher("select * from Win32_NetworkAdapterConfiguration");  
            foreach (ManagementObject nic in nisc.Get())   
            {  
                if (Convert.ToBoolean(nic["ipEnabled"]) == true)   
                {  
                    Console.WriteLine("{0} - {1}", nic["ServiceName"], nic["MACAddress"]);  
                }  
            }  
  
            Console.ReadKey();  
        }  
    }  
}  

Ping

廢話少說,具體代碼如下:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Net.NetworkInformation;  
  
namespace _20通過編程方式實現Ping  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            Ping ping = new Ping();  
            PingOptions pingOpt = new PingOptions();  
            pingOpt.DontFragment = true;//是否設置分段數據  
            string myInfo = "Hello, world!";  
            byte[] bufferInfo = Encoding.ASCII.GetBytes(myInfo);  
            int timeOut = 1200;  
            string ipTarget = "192.168.1.102";  
            PingReply pingReply = ping.Send(ipTarget, timeOut, bufferInfo);  
            if (pingReply.Status == IPStatus.Success)  
            {  
                Console.WriteLine("耗費時間 - {0}\n路由節點數 - {1}\n數據分段 - {2}\n緩沖區大小 - {3}", pingReply.RoundtripTime, //耗費時間  
                                     pingReply.Options.Ttl, //路由節點數  
                                     pingReply.Options.DontFragment ? "發生分段" : "沒有發生分段",//數據分段  
                                     pingReply.Buffer.Length//緩沖區大小  
                                     );  
            }  
            else   
            {  
                Console.WriteLine("無法ping通");  
            }  
  
  
            Console.ReadKey();  
        }  
    }  
}  
C# 實現ping的功能

 


免責聲明!

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



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