XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEExtension

【DLL】XFE各类拓展是一个C#的DLL库,旨在优化C#代码中常用语句的使用,并提供更简洁的访问方式,同时提供Xunit测试框架,快速搭建服务器/客户端,免费ChatGPTAPI接口,免费通讯服务器,XFE下载器,新增格式等

公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/XFEExtension

添加获取本地IP地址的方法 局域网探测器现在有default选项,自动获取本地IP并探测

5e865da
XFE工作室室长 <mail@xfegzs.com>
提交于

代码差异

3 个文件 +56 -7
Modified XFEExtension.NetCore/WebExtension/LANDeviceDetector/LANDeviceDetector.cs +26 -5
@@ -1,15 +1,14 @@
1 1 using System.Net;
2 2 using System.Net.NetworkInformation;
3 3 using XFEExtension.NetCore.DelegateExtension;
4 using XFEExtension.NetCore.WebExtension;
4 5
5 6 namespace XFEExtension.NetCore.WebExtension.LANDeviceDetector;
6 7
7 8 /// <summary>
8 9 /// 局域网设备探测器
9 10 /// </summary>
10 /// <param name="iPStart">网络地址搜索的起始位置</param>
11 /// <param name="timeOut">超时</param>
12 public class LANDeviceDetector(string iPStart = "192.168.1.*", int timeOut = 100)
11 public class LANDeviceDetector
13 12 {
14 13 /// <summary>
15 14 /// 找到设备
@@ -19,12 +18,12 @@ public class LANDeviceDetector(string iPStart = "192.168.1.*", int timeOut = 100
19 18 /// <summary>
20 19 /// 网络地址搜索的起始位置
21 20 /// </summary>
22 public string IPStart { get; set; } = iPStart;
21 public string IPStart { get; set; }
23 22
24 23 /// <summary>
25 24 /// 每个设备检测超时
26 25 /// </summary>
27 public int TimeOut { get; set; } = timeOut;
26 public int TimeOut { get; set; }
28 27
29 28 /// <summary>
30 29 /// 是否正在检测
@@ -36,6 +35,28 @@ public class LANDeviceDetector(string iPStart = "192.168.1.*", int timeOut = 100
36 35 /// </summary>
37 36 public List<LANDevice> FindDevices { get; set; } = [];
38 37
38 /// <summary>
39 /// 局域网设备探测器
40 /// </summary>
41 /// <param name="iPStart">网络地址搜索起始位置,default默认为本机设备所在网络频段</param>
42 /// <param name="timeOut">超时</param>
43 public LANDeviceDetector(string iPStart = "default", int timeOut = 100)
44 {
45 if(iPStart == "default")
46 {
47 var localIPAddress = WebExtension.GetLocalIPAddress();
48 if (localIPAddress is not null)
49 IPStart = $"{string.Join(".", localIPAddress.ToString().Split('.')[..3])}.*";
50 else
51 IPStart = "192.168.1.*";
52 }
53 else
54 {
55 IPStart = "192.168.1.*";
56 }
57 TimeOut = timeOut;
58 }
59
39 60 /// <summary>
40 61 /// 开始探测设备
41 62 /// </summary>
Modified XFEExtension.NetCore/WebExtension/WebExtension.cs +29 -1
@@ -1,4 +1,6 @@
1 using System.Net.Http;
1 using System.Net;
2 using System.Net.NetworkInformation;
3 using System.Net.Sockets;
2 4 using System.Text;
3 5 using XFEExtension.NetCore.TaskExtension;
4 6
@@ -32,6 +34,7 @@ public static class WebExtension
32 34 return null;
33 35 }
34 36 }
37
35 38 /// <summary>
36 39 /// 从URL获取字符串内容
37 40 /// </summary>
@@ -55,6 +58,7 @@ public static class WebExtension
55 58 return null;
56 59 }
57 60 }
61
58 62 /// <summary>
59 63 /// 从URL获取字符串内容
60 64 /// </summary>
@@ -79,6 +83,7 @@ public static class WebExtension
79 83 return null;
80 84 }
81 85 }
86
82 87 /// <summary>
83 88 /// 从URL获取字符串内容
84 89 /// </summary>
@@ -103,6 +108,7 @@ public static class WebExtension
103 108 return null;
104 109 }
105 110 }
111
106 112 /// <summary>
107 113 /// 从URL获取字符串内容
108 114 /// </summary>
@@ -128,6 +134,7 @@ public static class WebExtension
128 134 return null;
129 135 }
130 136 }
137
131 138 /// <summary>
132 139 /// 从URL获取字符串内容
133 140 /// </summary>
@@ -153,6 +160,7 @@ public static class WebExtension
153 160 return null;
154 161 }
155 162 }
163
156 164 /// <summary>
157 165 /// 通过给定的URL获取其文件名
158 166 /// </summary>
@@ -167,4 +175,24 @@ public static class WebExtension
167 175 else
168 176 return response.Content.Headers.ContentDisposition.FileNameStar;
169 177 }
178
179 /// <summary>
180 /// 获取当前正在使用的本机IP地址
181 /// </summary>
182 /// <returns>IP地址</returns>
183 public static IPAddress? GetLocalIPAddress()
184 {
185 foreach (var networkInterface in NetworkInterface.GetAllNetworkInterfaces())
186 {
187 if (networkInterface.NetworkInterfaceType == NetworkInterfaceType.Loopback ||
188 networkInterface.OperationalStatus != OperationalStatus.Up)
189 continue;
190 var ipProperties = networkInterface.GetIPProperties();
191 if (ipProperties.GatewayAddresses.Count > 0)
192 foreach (var ip in ipProperties.UnicastAddresses)
193 if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
194 return ip.Address;
195 }
196 return null;
197 }
170 198 }
Modified XFEExtension.NetCore/XFEExtension.NetCore.csproj +1 -1
@@ -32,7 +32,7 @@
32 32 <PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
33 33 <PackageLicenseFile>LICENSE</PackageLicenseFile>
34 34 <AssemblyOriginatorKeyFile>..\XFEstudio.pfx</AssemblyOriginatorKeyFile>
35 <Version>2.5.0</Version>
35 <Version>2.6.0</Version>
36 36 <Authors>XFEstudio</Authors>
37 37 </PropertyGroup>
38 38