瀏覽代碼

更科学的获取本地ip

懒得勤快 5 年之前
父節點
當前提交
54d361b5c3

+ 0 - 29
Masuit.Tools.Core/Hardware/SystemInfo.cs

@@ -5,11 +5,8 @@ using System.Collections;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.Diagnostics;
 using System.Management;
 using System.Management;
-using System.Net;
-using System.Net.Sockets;
 using System.Runtime.InteropServices;
 using System.Runtime.InteropServices;
 using System.Text;
 using System.Text;
-using System.Text.RegularExpressions;
 using System.Threading;
 using System.Threading;
 
 
 namespace Masuit.Tools.Hardware
 namespace Masuit.Tools.Hardware
@@ -523,32 +520,6 @@ namespace Masuit.Tools.Hardware
             }
             }
         }
         }
 
 
-        /// <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>  
         /// 运行一个控制台程序并返回其输出参数。  
         /// 运行一个控制台程序并返回其输出参数。  
         /// </summary>  
         /// </summary>  

+ 1 - 1
Masuit.Tools.Core/Masuit.Tools.Core.csproj

@@ -2,7 +2,7 @@
 
 
   <PropertyGroup>
   <PropertyGroup>
     <TargetFramework>netcoreapp3.0</TargetFramework>
     <TargetFramework>netcoreapp3.0</TargetFramework>
-    <Version>2.2.7.5</Version>
+    <Version>2.2.7.6</Version>
     <Authors>懒得勤快</Authors>
     <Authors>懒得勤快</Authors>
     <Company>masuit.com</Company>
     <Company>masuit.com</Company>
     <Description>包含一些常用的操作类,大都是静态类,加密解密,反射操作,硬件信息,字符串扩展方法,日期时间扩展操作,大文件拷贝,图像裁剪,html处理,验证码、NoSql等常用封装。
     <Description>包含一些常用的操作类,大都是静态类,加密解密,反射操作,硬件信息,字符串扩展方法,日期时间扩展操作,大文件拷贝,图像裁剪,html处理,验证码、NoSql等常用封装。

+ 13 - 20
Masuit.Tools.Core/Win32/Windows.cs

@@ -1,9 +1,11 @@
 using System;
 using System;
+using System.Collections.Generic;
 using System.Diagnostics;
 using System.Diagnostics;
+using System.Linq;
 using System.Management;
 using System.Management;
 using System.Net;
 using System.Net;
+using System.Net.NetworkInformation;
 using System.Text;
 using System.Text;
-using System.Text.RegularExpressions;
 using System.Threading;
 using System.Threading;
 using static System.String;
 using static System.String;
 
 
@@ -18,27 +20,18 @@ namespace Masuit.Tools.Win32
         /// 获取当前使用的IP  
         /// 获取当前使用的IP  
         /// </summary>  
         /// </summary>  
         /// <returns></returns>  
         /// <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>  
         /// <summary>  

+ 0 - 29
Masuit.Tools/Hardware/SystemInfo.cs

@@ -5,11 +5,8 @@ using System.Collections;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.Diagnostics;
 using System.Management;
 using System.Management;
-using System.Net;
-using System.Net.Sockets;
 using System.Runtime.InteropServices;
 using System.Runtime.InteropServices;
 using System.Text;
 using System.Text;
-using System.Text.RegularExpressions;
 using System.Threading;
 using System.Threading;
 
 
 namespace Masuit.Tools.Hardware
 namespace Masuit.Tools.Hardware
@@ -523,32 +520,6 @@ namespace Masuit.Tools.Hardware
             }
             }
         }
         }
 
 
-        /// <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>  
         /// 运行一个控制台程序并返回其输出参数。  
         /// 运行一个控制台程序并返回其输出参数。  
         /// </summary>  
         /// </summary>  

二進制
Masuit.Tools/Properties/AssemblyInfo.cs


+ 13 - 20
Masuit.Tools/Win32/Windows.cs

@@ -1,11 +1,13 @@
 using Masuit.Tools.Hardware;
 using Masuit.Tools.Hardware;
 using System;
 using System;
+using System.Collections.Generic;
 using System.Diagnostics;
 using System.Diagnostics;
+using System.Linq;
 using System.Management;
 using System.Management;
 using System.Net;
 using System.Net;
+using System.Net.NetworkInformation;
 using System.Runtime.InteropServices;
 using System.Runtime.InteropServices;
 using System.Text;
 using System.Text;
-using System.Text.RegularExpressions;
 using System.Threading;
 using System.Threading;
 using static System.String;
 using static System.String;
 
 
@@ -68,27 +70,18 @@ namespace Masuit.Tools.Win32
         /// 获取当前使用的IP  
         /// 获取当前使用的IP  
         /// </summary>  
         /// </summary>  
         /// <returns></returns>  
         /// <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>  
         /// <summary>  

+ 1 - 1
Masuit.Tools/package.nuspec

@@ -4,7 +4,7 @@
     <!--*-->
     <!--*-->
     <id>Masuit.Tools</id>
     <id>Masuit.Tools</id>
     <!--*-->
     <!--*-->
-    <version>2.2.7.5</version>
+    <version>2.2.7.6</version>
     <title>Masuit.Tools</title>
     <title>Masuit.Tools</title>
     <!--*-->
     <!--*-->
     <authors>masuit.com</authors>
     <authors>masuit.com</authors>

+ 2 - 4
Test/Program.cs

@@ -1,4 +1,4 @@
-using Masuit.Tools.Security;
+using Masuit.Tools.Win32;
 using System;
 using System;
 
 
 namespace Test
 namespace Test
@@ -7,9 +7,7 @@ namespace Test
     {
     {
         static void Main(string[] args)
         static void Main(string[] args)
         {
         {
-            RsaKey keys = RsaCrypt.GenerateRsaKeys();
-            Console.WriteLine(keys.PublicKey);
-            Console.WriteLine(keys.PrivateKey);
+            Console.WriteLine(Windows.GetLocalUsedIP());
         }
         }
     }
     }
 }
 }