溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C#獲取本機(jī)IP搜集的方法有哪些

發(fā)布時間:2021-02-24 14:13:24 來源:億速云 閱讀:289 作者:小新 欄目:編程語言

這篇文章主要介紹了C#獲取本機(jī)IP搜集的方法有哪些,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

C#獲取本機(jī)IP搜集整理7種方法的示例代碼分享

 1 private void GetIP()  
 2 {  
 3     string hostName = Dns.GetHostName();//本機(jī)名   
 4     //System.Net.IPAddress[] addressList = Dns.GetHostByName(hostName).AddressList;//會警告GetHostByName()已過期,我運(yùn)行時且只返回了一個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 }

②使用IPHostEntry獲取本機(jī)局域網(wǎng)地址

1         static string GetLocalIp()  
2         {  
3             string hostname = Dns.GetHostName();//得到本機(jī)名   
4             //IPHostEntry localhost = Dns.GetHostByName(hostname);//方法已過期,只得到IPv4的地址   
5 <SPAN style="WHITE-SPACE: pre"> </SPAN>    IPHostEntry localhost = Dns.GetHostEntry(hostname);  
6             IPAddress localaddr = localhost.AddressList[0];  
7             return localaddr.ToString();  
8         }

③獲取本機(jī)網(wǎng)絡(luò)ip地址

方法時通過向網(wǎng)站向一些提供IP查詢的網(wǎng)站發(fā)送webrequest,然后分析返回的數(shù)據(jù)流

 1        string strUrl = "提供IP查詢的網(wǎng)站的鏈接";  
 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.php.cn/    
12         (這種鏈接很容易找的,百度“IP”得到一些網(wǎng)站,分析一下網(wǎng)站的鏈接就能得到) 
13         返回的數(shù)據(jù)是:  
14         <p class="well"><p>當(dāng)前 IP:<code>0.0.0.0</code>&nbsp;來自:XX省XX市 電信</p><p>GeoIP: Beijing, China</p></p>  
15         解析這段就行  
16       */

④//由于用到了ManagementClass、ManagementObjectCollection;必須添加引用System.Management.dll及using System.Management;

 1 private void GetIP2()  
 2         {  
 3             string stringMAC = "";  
 4             string stringIP = "";  
 5             ManagementClass managementClass = new ManagementClass("Win32_NetworkAdapterConfiguration");  
 6             ManagementObjectCollection managementObjectCollection = managementClass.GetInstances();  
 7             foreach(ManagementObject managementObject in managementObjectCollection)  
 8             {  
 9                 if ((bool)managementObject["IPEnabled"] == true)  
10                 {  
11                     stringMAC += managementObject["MACAddress"].ToString();  
12                     string[] IPAddresses = (string[])managementObject["IPAddress"];  
13                     if (IPAddresses.Length > 0)  
14                     {  
15                         stringIP = IPAddresses[0];   
16                     }  
17                 }  
18             }  
19             txtMAC.Text = stringMAC.ToString();  
20             txtIP.Text = stringIP.ToString();  
21         }

⑤調(diào)用一個網(wǎng)站提供的Web服務(wù)來查詢IP網(wǎng)址
弄了好半天,但是沒學(xué)會怎樣調(diào)用Web Service,按照搜到的頁面做也不行,遂先放棄吧.....畢竟還未接觸到WebService,改天把WebService搞定再來一定就容易了(留待以后完善吧)

⑥通過獲取CMD里ipconfig命令的結(jié)果來得到IP

 1    private void GetIP6()  
 2    {  
 3        Process cmd = new Process();  
 4        cmd.StartInfo.FileName = "ipconfig.exe";//設(shè)置程序名    5        cmd.StartInfo.Arguments = "/all";  //參數(shù)   
 6 //重定向標(biāo)準(zhǔn)輸出    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        關(guān)于: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    }

C#獲取本機(jī)IP搜集的方法有哪些

⑦NetworkInformation

 1 private void GetIP5()  
 2        {  
 3     //需要的命名空間   
 4            //using System.Net.NetworkInformation;   
 5            //using System.Net.Sockets;    6            string str = "";  
 7            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();  
 8            int i = 0;  
 9            foreach (NetworkInterface adapter in adapters)  
10            {  
11   12                IPInterfaceProperties adapterProperties = adapter.GetIPProperties();  
13                UnicastIPAddressInformationCollection allAddress =  
14 adapterProperties.UnicastAddresses;  
15                if (allAddress.Count > 0)  
16                {  
17                    str += "interface   " + i + "description:\n\t " + adapter.Description + "\n ";  
18                    i++;  
19                    foreach (UnicastIPAddressInformation addr in allAddress)  
20                    {  
21                        if (addr.Address.AddressFamily == AddressFamily.InterNetworkV6)  
22                        {  
23                            ipListComb.Items.Add(addr.Address);  
24                        }  
25                        if (addr.Address.AddressFamily == AddressFamily.InterNetwork)  
26                        {  
27                            comboBox1.Items.Add(addr.Address);  
28                        }  
29   30                    }  
31                }  
32            }  
33            MessageBox.Show(str);  
34        }

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“C#獲取本機(jī)IP搜集的方法有哪些”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

ip
AI