You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.2 KiB
C#

1 year ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;
using System.Net;
namespace Mesnac.Basic
{
/// <summary>
/// Windows进程辅助类
/// </summary>
public class ProcessHelper
{
/// <summary>
/// 判断是否已经存在运行的实例
/// </summary>
/// <returns>存在返回true不存在返回false</returns>
public static bool HaveRunningInstance()
{
System.Diagnostics.Process current = System.Diagnostics.Process.GetCurrentProcess();
System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName(current.ProcessName);
if (processes.Length >= 2)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 查询指定名称的进程是否存在
/// </summary>
/// <param name="processName">要查找的进程名称</param>
/// <returns>存在返回true不存在返回false</returns>
public static bool FindByProcessName(string processName)
{
System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName(processName);
if (processes == null || processes.Length == 0)
{
return false;
}
else
{
return true;
}
}
/// <summary>
/// 检测端口是否被占用
/// </summary>
/// <param name="port">要检测的端口号</param>
/// <returns>占用返回true未被占用则返回false</returns>
public static bool PortInUse(int port)
{
bool inUse = false;
IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners();
foreach (IPEndPoint endPoint in ipEndPoints)
{
if (endPoint.Port == port)
{
inUse = true;
break;
}
}
return inUse;
}
}
}