C#定位IP地址归属地及运营商等

一、通过NuGet安装IP2Region(支持Framework)如果是Net6则可以使用IP2Region.Net版本

1、IP2Region使用方式:

 using (DbSearcher search = new DbSearcher(CommonHelper.MapPath("/Resource/ip2region.db")))
   {
         IP2Region.Models.DataBlock ipModel = search.MemorySearch(ip);
         string ipRegion = ipModel.Region;   //ipModel.Region =  "国家|区域|省份|城市|ISP"
         if (!string.IsNullOrEmpty(ipRegion))
             res= ipRegion;
  }

2、IP2Region.Net使用方式:

ISearcher searcher = new Searcher(new CachePolicy(), Environment.CurrentDirectory + @"\Resource\ip2region.xdb");
string ipRegion = searcher.Search(ip) ?? string.Empty;

//注入ip2region所需服务

//Program.cs
services.AddSingleton<ISearcher>(new Searcher(new CachePolicy(), Environment.CurrentDirectory + @"\Lib\ip2region.xdb"));

 

二、下载“ip2region.db和ip2region.xdb

1、https://files.cnblogs.com/files/firstcsharp/ip2region.db.rar?t=1713425401&download=true

2、 https://gitee.com/lionsoul/ip2region#/lionsoul/ip2region/blob/master/binding/csharp 或 https://files.cnblogs.com/files/firstcsharp/ip2region.xdb.rar?t=1713425406&download=true

posted @ 2024-04-18 15:31  James·wang  阅读(338)  评论(0编辑  收藏  举报