Skip to content

Commit

Permalink
更科学的获取本地ip
Browse files Browse the repository at this point in the history
  • Loading branch information
ldqk committed Dec 20, 2019
1 parent 4692765 commit 54d361b
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 104 deletions.
29 changes: 0 additions & 29 deletions Masuit.Tools.Core/Hardware/SystemInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Management;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;

namespace Masuit.Tools.Hardware
Expand Down Expand Up @@ -523,32 +520,6 @@ public static IList<string> GetIPAddress()
}
}

/// <summary>
/// 获取当前使用的IP
/// </summary>
/// <returns></returns>
public static string GetLocalUsedIP()
{
string result = RunApp("route", "print", true);
var m = Regex.Match(result, @"0.0.0.0\s+0.0.0.0\s+(\d+.\d+.\d+.\d+)\s+(\d+.\d+.\d+.\d+)");
if (m.Success)
{
return m.Groups[2].Value;
}

try
{
using var c = new TcpClient();
c.Connect("www.baidu.com", 80);
var ip = ((IPEndPoint)c.Client.LocalEndPoint).Address.ToString();
return ip;
}
catch (Exception)
{
return "未能获取当前使用的IP,可能是当前程序无管理员权限,如果是web应用程序,请将应用程序池的高级设置中的进程模型下的标识设置为:LocalSystem;如果是普通桌面应用程序,请提升管理员权限后再操作。";
}
}

/// <summary>
/// 运行一个控制台程序并返回其输出参数。
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Masuit.Tools.Core/Masuit.Tools.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<Version>2.2.7.5</Version>
<Version>2.2.7.6</Version>
<Authors>懒得勤快</Authors>
<Company>masuit.com</Company>
<Description>包含一些常用的操作类,大都是静态类,加密解密,反射操作,硬件信息,字符串扩展方法,日期时间扩展操作,大文件拷贝,图像裁剪,html处理,验证码、NoSql等常用封装。
Expand Down
33 changes: 13 additions & 20 deletions Masuit.Tools.Core/Win32/Windows.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Management;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using static System.String;

Expand All @@ -18,27 +20,18 @@ public static class Windows
/// 获取当前使用的IP
/// </summary>
/// <returns></returns>
public static string GetLocalUsedIP()
public static IPAddress GetLocalUsedIP()
{
string result = RunApp("route", "print", true);
var m = Regex.Match(result, @"0.0.0.0\s+0.0.0.0\s+(\d+.\d+.\d+.\d+)\s+(\d+.\d+.\d+.\d+)");
if (m.Success)
{
return m.Groups[2].Value;
}

try
{
using var c = new System.Net.Sockets.TcpClient();
c.Connect("www.baidu.com", 80);
var ip = ((IPEndPoint)c.Client.LocalEndPoint).Address.ToString();
return NetworkInterface.GetAllNetworkInterfaces().Select(p => p.GetIPProperties()).SelectMany(p => p.UnicastAddresses).Where(p => p.DuplicateAddressDetectionState == DuplicateAddressDetectionState.Preferred && !IPAddress.IsLoopback(p.Address)).Select(x => x.Address).FirstOrDefault();
}

return ip;
}
catch (Exception)
{
return null;
}
/// <summary>
/// 获取本机所有的ip地址
/// </summary>
/// <returns></returns>
public static List<UnicastIPAddressInformation> GetLocalIPs()
{
return NetworkInterface.GetAllNetworkInterfaces().Select(p => p.GetIPProperties()).SelectMany(p => p.UnicastAddresses).Where(p => !IPAddress.IsLoopback(p.Address)).ToList();
}

/// <summary>
Expand Down
29 changes: 0 additions & 29 deletions Masuit.Tools/Hardware/SystemInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Management;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;

namespace Masuit.Tools.Hardware
Expand Down Expand Up @@ -523,32 +520,6 @@ public static IList<string> GetIPAddress()
}
}

/// <summary>
/// 获取当前使用的IP
/// </summary>
/// <returns></returns>
public static string GetLocalUsedIP()
{
string result = RunApp("route", "print", true);
var m = Regex.Match(result, @"0.0.0.0\s+0.0.0.0\s+(\d+.\d+.\d+.\d+)\s+(\d+.\d+.\d+.\d+)");
if (m.Success)
{
return m.Groups[2].Value;
}

try
{
using var c = new TcpClient();
c.Connect("www.baidu.com", 80);
var ip = ((IPEndPoint)c.Client.LocalEndPoint).Address.ToString();
return ip;
}
catch (Exception)
{
return "未能获取当前使用的IP,可能是当前程序无管理员权限,如果是web应用程序,请将应用程序池的高级设置中的进程模型下的标识设置为:LocalSystem;如果是普通桌面应用程序,请提升管理员权限后再操作。";
}
}

/// <summary>
/// 运行一个控制台程序并返回其输出参数。
/// </summary>
Expand Down
Binary file modified Masuit.Tools/Properties/AssemblyInfo.cs
Binary file not shown.
33 changes: 13 additions & 20 deletions Masuit.Tools/Win32/Windows.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using Masuit.Tools.Hardware;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Management;
using System.Net;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using static System.String;

Expand Down Expand Up @@ -68,27 +70,18 @@ public static void ClearMemorySilent()
/// 获取当前使用的IP
/// </summary>
/// <returns></returns>
public static string GetLocalUsedIP()
public static IPAddress GetLocalUsedIP()
{
string result = RunApp("route", "print", true);
var m = Regex.Match(result, @"0.0.0.0\s+0.0.0.0\s+(\d+.\d+.\d+.\d+)\s+(\d+.\d+.\d+.\d+)");
if (m.Success)
{
return m.Groups[2].Value;
}

try
{
using var c = new System.Net.Sockets.TcpClient();
c.Connect("www.baidu.com", 80);
var ip = ((IPEndPoint)c.Client.LocalEndPoint).Address.ToString();
return NetworkInterface.GetAllNetworkInterfaces().Select(p => p.GetIPProperties()).SelectMany(p => p.UnicastAddresses).Where(p => p.DuplicateAddressDetectionState == DuplicateAddressDetectionState.Preferred && !IPAddress.IsLoopback(p.Address)).Select(x => x.Address).FirstOrDefault();
}

return ip;
}
catch (Exception)
{
return null;
}
/// <summary>
/// 获取本机所有的ip地址
/// </summary>
/// <returns></returns>
public static List<UnicastIPAddressInformation> GetLocalIPs()
{
return NetworkInterface.GetAllNetworkInterfaces().Select(p => p.GetIPProperties()).SelectMany(p => p.UnicastAddresses).Where(p => !IPAddress.IsLoopback(p.Address)).ToList();
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Masuit.Tools/package.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<!--*-->
<id>Masuit.Tools</id>
<!--*-->
<version>2.2.7.5</version>
<version>2.2.7.6</version>
<title>Masuit.Tools</title>
<!--*-->
<authors>masuit.com</authors>
Expand Down
6 changes: 2 additions & 4 deletions Test/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Masuit.Tools.Security;
using Masuit.Tools.Win32;
using System;

namespace Test
Expand All @@ -7,9 +7,7 @@ static class Program
{
static void Main(string[] args)
{
RsaKey keys = RsaCrypt.GenerateRsaKeys();
Console.WriteLine(keys.PublicKey);
Console.WriteLine(keys.PrivateKey);
Console.WriteLine(Windows.GetLocalUsedIP());
}
}
}

0 comments on commit 54d361b

Please sign in to comment.