thrownewException($"IP initialize failed. Can not find database file from {DbPath}. Please download the file to your application root directory, then set it can be copied to the output directory. Url: https://gitee.com/lionsoul/ip2region/blob/master/data/ip2region.xdb");
}
Searcher=newSearcher(CachePolicy.File,DbPath);
}
publicstaticstringGetRegion(stringip)
{
if(string.IsNullOrEmpty(ip))
{
thrownewArgumentException("IP为空",nameof(ip));
}
try
{
varregion=Searcher.Search(ip);
returnregion;
}
catch(Exceptionex)
{
Console.WriteLine(ex.Message);
thrownewException($"搜索IP异常IP={ip}",ex);
}
}
publicstaticIpInfoSearch(stringip)
{
try
{
varregion=GetRegion(ip);
vararray=region.Split("|");
varinfo=newIpInfo()
{
Country=array[0],
Province=array[2],
City=array[3],
NetworkOperator=array[4],
IpAddress=ip
};
returninfo;
}
catch(Exceptione)
{
thrownewException("Error converting ip address information to ipinfo object",e);