72 lines
2.0 KiB
C#
72 lines
2.0 KiB
C#
using System.Net;
|
||
using System.Net.NetworkInformation;
|
||
|
||
namespace PLCSharp.Core.Tools
|
||
{
|
||
/// <summary>
|
||
/// Net工具
|
||
/// </summary>
|
||
public static class NetTool
|
||
{
|
||
private static readonly Ping Ping = new();
|
||
|
||
/// <summary>
|
||
/// PingIPAsync
|
||
/// </summary>
|
||
/// <param name="ip">ip</param>
|
||
/// <returns>返回结果</returns>
|
||
public static async Task<bool> PingIPAsync(string ip)
|
||
{
|
||
var isip = IPAddress.TryParse(ip, out var ipAddress);
|
||
if (isip == false)
|
||
{
|
||
return false;
|
||
}
|
||
bool isMatch = ip.IsIP(); // 统一走 StringExtension.IsIP(修复后四段0-255),消除复制粘贴正则
|
||
if (isMatch == false)
|
||
{
|
||
return false;
|
||
}
|
||
try
|
||
{
|
||
var result = await Ping.SendPingAsync(ipAddress);
|
||
return result.Status == IPStatus.Success;
|
||
}
|
||
catch
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// PingIP
|
||
/// </summary>
|
||
/// <param name="ip">ip</param>
|
||
/// <returns>返回布尔值</returns>
|
||
public static bool PingIP(string ip)
|
||
{
|
||
lock (Ping)
|
||
{
|
||
var isip = IPAddress.TryParse(ip, out var ipAddress);
|
||
if (isip == false)
|
||
{
|
||
return false;
|
||
}
|
||
bool isMatch = ip.IsIP(); // 统一走 StringExtension.IsIP(修复后四段0-255),消除复制粘贴正则
|
||
if (isMatch == false)
|
||
{
|
||
return false;
|
||
}
|
||
try
|
||
{
|
||
var result = Ping.Send(ipAddress);
|
||
return result.Status == IPStatus.Success;
|
||
}
|
||
catch
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
} |