C#獲取本機(jī)IP地址(ipv4)
獲取本機(jī)所有IP地址:
這些地址是包含所有網(wǎng)卡(虛擬網(wǎng)卡)的ipv4和ipv6地址。
string name = Dns.GetHostName(); IPAddress[] ipadrlist = Dns.GetHostAddresses(name);
獲取本機(jī)所有IPV4地址:
string name = Dns.GetHostName();
IPAddress[] ipadrlist = Dns.GetHostAddresses(name);
foreach (IPAddress ipa in ipadrlist)
{
if (ipa.AddressFamily == AddressFamily.InterNetwork)
Console.Writeline(ipa.ToString());
}
若要單單獲取ipv4地址,可以用IPAdress.AddressFamily 屬性判斷:對(duì)于 IPv4,返回 InterNetwork;對(duì)于 IPv6,返回 InterNetworkV6。
然而如果本機(jī)可能有多個(gè)ipv4的地址,那如何獲取訪問(wèn)默認(rèn)網(wǎng)關(guān)時(shí)使用的網(wǎng)卡IP呢。在CSDN論壇找到了大神的方法,用的是查詢(xún)本機(jī)路由表。
獲取本機(jī)正在使用的ipv4地址(訪問(wèn)互聯(lián)網(wǎng)的IP)
可別小看,還是有很多需要考慮的:
1.一個(gè)電腦有多個(gè)網(wǎng)卡,有線的、無(wú)線的、還有vmare虛擬的兩個(gè)網(wǎng)卡。
2.就算只有一個(gè)網(wǎng)卡,但是該網(wǎng)卡配置了N個(gè)IP地址.其中還包括ipv6地址。
/// <summary>
/// 獲取當(dāng)前使用的IP
/// </summary>
/// <returns></returns>
public static string GetLocalIP()
{
string result = RunApp("route", "print",true);
Match m = 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 c = new System.Net.Sockets.TcpClient();
c.Connect("www.baidu.com", 80);
string ip = ((System.Net.IPEndPoint)c.Client.LocalEndPoint).Address.ToString();
c.Close();
return ip;
}
catch (Exception)
{
return null;
}
}
}
/// <summary>
/// 獲取本機(jī)主DNS
/// </summary>
/// <returns></returns>
public static string GetPrimaryDNS()
{
string result = RunApp("nslookup", "",true);
Match m = Regex.Match(result, @"\d+\.\d+\.\d+\.\d+");
if (m.Success)
{
return m.Value;
}
else
{
return null;
}
}
/// <summary>
/// 運(yùn)行一個(gè)控制臺(tái)程序并返回其輸出參數(shù)。
/// </summary>
/// <param name="filename">程序名</param>
/// <param name="arguments">輸入?yún)?shù)</param>
/// <returns></returns>
public static string RunApp(string filename, string arguments,bool recordLog)
{
try
{
if (recordLog)
{
Trace.WriteLine(filename + " " + arguments);
}
Process proc = new Process();
proc.StartInfo.FileName = filename;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.Arguments = arguments;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.Start();
using (System.IO.StreamReader sr = new System.IO.StreamReader(proc.StandardOutput.BaseStream, Encoding.Default))
{
//string txt = sr.ReadToEnd();
//sr.Close();
//if (recordLog)
//{
// Trace.WriteLine(txt);
//}
//if (!proc.HasExited)
//{
// proc.Kill();
//}
//上面標(biāo)記的是原文,下面是我自己調(diào)試錯(cuò)誤后自行修改的
Thread.Sleep(100); //貌似調(diào)用系統(tǒng)的nslookup還未返回?cái)?shù)據(jù)或者數(shù)據(jù)未編碼完成,程序就已經(jīng)跳過(guò)直接執(zhí)行
//txt = sr.ReadToEnd()了,導(dǎo)致返回的數(shù)據(jù)為空,故睡眠令硬件反應(yīng)
if (!proc.HasExited) //在無(wú)參數(shù)調(diào)用nslookup后,可以繼續(xù)輸入命令繼續(xù)操作,如果進(jìn)程未停止就直接執(zhí)行
{ //txt = sr.ReadToEnd()程序就在等待輸入,而且又無(wú)法輸入,直接掐住無(wú)法繼續(xù)運(yùn)行
proc.Kill();
}
string txt = sr.ReadToEnd();
sr.Close();
if (recordLog)
Trace.WriteLine(txt);
return txt;
}
}
catch (Exception ex)
{
Trace.WriteLine(ex);
return ex.Message;
}
}
另有一種方法通過(guò)用ipconfig來(lái)獲取:
private void GetIP()
{
Process cmd = new Process();
cmd.StartInfo.FileName = "ipconfig.exe";//設(shè)置程序名
cmd.StartInfo.Arguments = "/all"; //參數(shù)
//重定向標(biāo)準(zhǔn)輸出
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.CreateNoWindow = true;//不顯示窗口(控制臺(tái)程序是黑屏)
//cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//暫時(shí)不明白什么意思
/*
收集一下 有備無(wú)患
關(guān)于:ProcessWindowStyle.Hidden隱藏后如何再顯示?
hwndWin32Host = Win32Native.FindWindow(null, win32Exinfo.windowsName);
Win32Native.ShowWindow(hwndWin32Host, 1); //先FindWindow找到窗口后再ShowWindow
*/
cmd.Start();
string info = cmd.StandardOutput.ReadToEnd();
cmd.WaitForExit();
cmd.Close();
textBox1.AppendText(info);
}
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
詳解C#如何解決程序卡頓的問(wèn)題(多線程初步學(xué)習(xí))
在編寫(xiě)程序的時(shí)候,有時(shí)候難免會(huì)出現(xiàn)后臺(tái)運(yùn)行時(shí)間過(guò)長(zhǎng)的問(wèn)題,這個(gè)時(shí)候就要考慮多線程的操作了,所以本文給大家介紹了C#解決程序卡頓問(wèn)題的方法,需要的朋友可以參考下2024-04-04
C# 控件屬性和InitializeComponent()關(guān)系案例詳解
這篇文章主要介紹了C# 控件屬性和InitializeComponent()關(guān)系案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
C#中winform窗體實(shí)現(xiàn)注冊(cè)/登錄功能實(shí)例(DBHelper類(lèi))
在編寫(xiě)項(xiàng)目時(shí),編寫(xiě)了一部分關(guān)于登錄頁(yè)面的一些代碼,下面這篇文章主要給大家介紹了關(guān)于C#中winform窗體實(shí)現(xiàn)注冊(cè)/登錄功能(DBHelper類(lèi))的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06
C#實(shí)現(xiàn)將DataTable內(nèi)容輸出到Excel表格的方法
這篇文章主要介紹了C#實(shí)現(xiàn)將DataTable內(nèi)容輸出到Excel表格的方法,較為詳細(xì)的分析了C#基于DataTable保存Excel數(shù)據(jù)的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08

