C#-Offline IP location library based on ip2 region

C#-Offline IP location library based on ip2 region

During development, we need to record the ip and location information about the login person, which can be achieved through the ip2 region!

最后更新 7/2/2022 10:01 AM
黑哥聊dotNet
预计阅读 2 分钟
分类
.NET
标签
.NET C#

preface

During development, we need to record the ip and location information about the login person, which can be achieved through the ip2 region!

For example:

  • When qq, gitee, etc. log in a different place, they will remind us to log in a different place!
  • Software such as takeout requires geographical location. If our gps is turned off, we can also analyze geographical location based on IP

profile

ip2 region -Offline IP address location library with an accuracy rate of 99.9%, 0.0x millisecond queries, ip2region.db database only has a few MB, and provides query binding algorithms such as java,php,c,python,nodejs,golang, and c#, and Binary, B-tree, and memory. Each IP data segment has a fixed format:

use

  • nuget installation
Install-Package IP2Region
  • Download ip2region.db
git clone https://github.com/lionsoul2014/ip2region.git

然后到 data/文件夹下面找到ip2region.db复制到项目resources

It's very simple to use, only one line of code

static void Main(string[] args)
{
  using (var _search = new DbSearcher(Environment.CurrentDirectory + @"\DB\ip2region.db"))
  {
   Console.WriteLine(_search.MemorySearch("183.129.193.166").Region);
   Console.Read();
  }
}

summary

The article comes from improvisation. Although I haven't encountered this kind of need in my work yet, it is always beneficial to understand it. Everyone is welcome to leave a message to discuss it!

Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 4/22/2026

Support for. NET by operating system versions (250707 update)

Use virtual machines and test machines to test the support of each version of the operating system for. NET. After installing the operating system, it is passed by measuring the corresponding running time of the installation and being able to run the Stardust Agent.

继续阅读
同分类 / 同标签 2/7/2026

Summary of experience in using AOT

From the very beginning of project creation, you should develop a good habit of conducting AOT release testing in a timely manner whenever new features are added or newer syntax is used.

继续阅读