最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

1.第一種方式

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

/// <summary>
  /// 網(wǎng)絡(luò)不通暢可以獲取
  /// 不過能獲取到具體的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)絡(luò)的影響,如果沒有連接到網(wǎng)絡(luò),本地配置的部分IP是獲取不到的,我也遇到一種情況是,電腦環(huán)境正常,就是獲取不到,原因至今還不知道;

2.第二種方式

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

/// <summary>
  /// 只有網(wǎng)絡(luò)通暢才能獲取
  /// </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)絡(luò)的約束,沒有聯(lián)網(wǎng)的狀態(tài)下不一定能夠獲取到IP;

3.第三種方式

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

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,如果在本機(jī)電腦沒有聯(lián)網(wǎng)的狀態(tài)下,也是獲取不到IP的,并且也不太建議使用這種方式;

4.第四種方法

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

/// <summary>
  /// 無論網(wǎng)絡(luò)通不通都能獲取到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)中。

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

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

相關(guān)文章

最新評論

革吉县| 西华县| 佳木斯市| 嵩明县| 平和县| 宝鸡市| 隆尧县| 襄垣县| 和田县| 沾益县| 沾益县| 桑植县| 和龙市| 甘南县| 永安市| 泾源县| 霸州市| 碌曲县| 禹城市| 博野县| 安福县| 怀仁县| 米脂县| 文安县| 卓资县| 胶州市| 水城县| 泸定县| 贵南县| 龙井市| 福州市| 浏阳市| 新安县| 铜陵市| 原阳县| 苍山县| 油尖旺区| 治多县| 定边县| 扶风县| 陆良县|