亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#獲取本地IP的四種方式示例詳解

 更新時間:2020年07月21日 11:43:05   作者:Koalin  
這篇文章主要介紹了C#獲取本地IP的四種方式示例詳解, 文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

1.第一種方式

采用System.Net.Dns的GetHostAddress的方式,具體請看代碼:

/// <summary>
  /// 網(wǎng)絡不通暢可以獲取
  /// 不過能獲取到具體的IP
  /// </summary>
  /// <returns></returns>
  public static List<IPAddress> GetByGetHostAddresses()
  {
   try
   {
    IPAddress[] adds = Dns.GetHostAddresses(Dns.GetHostName());
    return adds == null || adds.Length == 0 ? new List<IPAddress>() : adds.ToList<IPAddress>();
   }
   catch (Exception)
   {
    return new List<IPAddress>();

   }
  }

這種方式受到網(wǎng)絡的影響,如果沒有連接到網(wǎng)絡,本地配置的部分IP是獲取不到的,我也遇到一種情況是,電腦環(huán)境正常,就是獲取不到,原因至今還不知道;

2.第二種方式

采用System.Management.ManagementClass來獲取,詳細請看代碼:

/// <summary>
  /// 只有網(wǎng)絡通暢才能獲取
  /// </summary>
  /// <returns></returns>
  public static List<IPAddress> GetByManagementClass()
  {
   try
   {
    ManagementClass mClass = new System.Management.ManagementClass("Win32_NetworkAdapterConfiguration");
    ManagementObjectCollection managementObjectCollection = mClass.GetInstances();
    List<IPAddress> ls = new List<IPAddress>();
    foreach (var item in managementObjectCollection)
    {
     if ((bool)item["IPEnabled"] == true)
     {
      foreach (var ip in (string[])item["IPAddress"])
      {
       IPAddress ipout = null;
       IPAddress.TryParse(ip, out ipout);
       if (ipout != null)
       {

        ls.Add(ipout);
       }
      }
     }
    }
    return ls;
   }
   catch (Exception)
   {
    return new List<IPAddress>();

   }
  }

同樣的這種方式也受到網(wǎng)絡的約束,沒有聯(lián)網(wǎng)的狀態(tài)下不一定能夠獲取到IP;

3.第三種方式

我們平時在命令行中輸入ipconfig命令同樣也是能獲取,在程序中啟動Ipconfig應用程序,然后解析出來,也是可以獲取得到IP,詳細請看代碼:

public static List<IPAddress> GetByCMD()
  {
   try
   {
    Process cmd = new Process();
    cmd.StartInfo.FileName = "ipconfig.exe";
    cmd.StartInfo.Arguments = "/all";
    cmd.StartInfo.RedirectStandardOutput = true;
    cmd.StartInfo.RedirectStandardInput = true;
    cmd.StartInfo.UseShellExecute = false;
    cmd.StartInfo.CreateNoWindow = true;
    cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    cmd.Start();
    string info = "";
    List<IPAddress> ls = new List<IPAddress>();
    // info = cmd.StandardOutput.ReadToEnd();
    Regex validipregex = new Regex(@"\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}");
    //new Regex(@"^(([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");
    while ((info = cmd.StandardOutput.ReadLine()) != null)
    {
     IPAddress ip = null;
     Console.WriteLine(info);
     info = validipregex.Match(info).Value;

     IPAddress.TryParse(info, out ip);

     if (ip != null)
     {
      ls.Add(ip);
     }
    }

    cmd.WaitForExit();
    cmd.Close();
    return ls;
   }
   catch (Exception)
   {
    return new List<IPAddress>();

   }
  }

即便是通過這種方式來獲取IP,如果在本機電腦沒有聯(lián)網(wǎng)的狀態(tài)下,也是獲取不到IP的,并且也不太建議使用這種方式;

4.第四種方法

采用NetworkInterface.GetAllNetworkInterfaces的方式是不受網(wǎng)絡的影響的,聯(lián)網(wǎng)或者不聯(lián)網(wǎng)都能夠獲取到IP,詳細請看代碼:

/// <summary>
  /// 無論網(wǎng)絡通不通都能獲取到Ip
  /// </summary>
  /// <returns></returns>
  public static List<IPAddress> GetByNetworkInterface()
  {
   try
   {
    NetworkInterface[] intf = NetworkInterface.GetAllNetworkInterfaces();
    List<IPAddress> ls = new List<IPAddress>();
    foreach (var item in intf)
    {
     IPInterfaceProperties adapterPropertis = item.GetIPProperties();
     UnicastIPAddressInformationCollection coll = adapterPropertis.UnicastAddresses;
     foreach (var col in coll)
     {
      ls.Add(col.Address);
     }
    }
    return ls;
   }
   catch (Exception)
   {
    return new List<IPAddress>();

   }
  }

以上所說的聯(lián)網(wǎng),包括連接在局域網(wǎng)中。

希望給有需要的朋友們帶來幫助;

到此這篇關于C#獲取本地IP的四種方式示例詳解的文章就介紹到這了,更多相關C#獲取本地IP內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 在C#中對TCP客戶端的狀態(tài)封裝詳解

    在C#中對TCP客戶端的狀態(tài)封裝詳解

    本篇文章小編為大家介紹,在C#中對TCP客戶端的狀態(tài)封裝詳解,需要的朋友參考下
    2013-04-04
  • C# Onnx實現(xiàn)特征匹配DeDoDe檢測

    C# Onnx實現(xiàn)特征匹配DeDoDe檢測

    這篇文章主要為大家詳細介紹了C# Onnx如何實現(xiàn)特征匹配DeDoDe檢測,文中的示例代碼講解詳細,具有一定的借鑒價值,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-11-11
  • C#中Forms.Timer、Timers.Timer、Threading.Timer的用法分析

    C#中Forms.Timer、Timers.Timer、Threading.Timer的用法分析

    這篇文章主要介紹了C#中Forms.Timer、Timers.Timer、Threading.Timer的用法分析,以實例形式較為詳細的講述了.NET Framework里面提供的三種Timer具體用法,需要的朋友可以參考下
    2014-10-10
  • C#通過不安全代碼看內(nèi)存加載的示例詳解

    C#通過不安全代碼看內(nèi)存加載的示例詳解

    C#中類型分為值類型和引用類型。這篇文章將用不安全代碼的地址,來看一下值類型和引用類型的存儲,文中的示例代碼講解詳細,感興趣的可以了解一下
    2022-07-07
  • 淺談C#設計模式之代理模式

    淺談C#設計模式之代理模式

    這篇文章主要介紹了淺談C#設計模式之代理模式,需要的朋友可以參考下
    2014-12-12
  • c#中返回文章發(fā)表的時間差的示例

    c#中返回文章發(fā)表的時間差的示例

    現(xiàn)在是2012-12-04 11:29:59,發(fā)表時間是:2012-12-02 21:29:59,傳統(tǒng)的ts.Days因為值為1天14小時0分0秒,會返回“昨天”,而這個會返回“前天”
    2012-12-12
  • C#裝飾器模式(Decorator Pattern)實例教程

    C#裝飾器模式(Decorator Pattern)實例教程

    這篇文章主要介紹了C#裝飾器模式(Decorator Pattern),以一個完整實例形式講述了C#裝飾器模式的實現(xiàn)過程,有助于深入理解C#程序設計思想,需要的朋友可以參考下
    2014-09-09
  • DevExpress實現(xiàn)禁用TreeListNode CheckBox的方法

    DevExpress實現(xiàn)禁用TreeListNode CheckBox的方法

    這篇文章主要介紹了DevExpress實現(xiàn)禁用TreeListNode CheckBox的方法,在項目開發(fā)中有應用價值,需要的朋友可以參考下
    2014-08-08
  • c# 抓取Web網(wǎng)頁數(shù)據(jù)分析

    c# 抓取Web網(wǎng)頁數(shù)據(jù)分析

    通過程序自動的讀取其它網(wǎng)站網(wǎng)頁顯示的信息,類似于爬蟲程序。比方說我們有一個系統(tǒng),要提取BaiDu網(wǎng)站上歌曲搜索排名。分析系統(tǒng)在根據(jù)得到的數(shù)據(jù)進行數(shù)據(jù)分析。為業(yè)務提供參考數(shù)據(jù)。
    2008-11-11
  • C#中常用窗口特效的實現(xiàn)代碼

    C#中常用窗口特效的實現(xiàn)代碼

    這篇文章主要為大家詳細介紹了C#中三個常用的窗口特效的實現(xiàn),分別是淡入淡出、變大變小、緩升緩降,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-12-12

最新評論