using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;
using System.Net;
namespace Mesnac.Basic
{
///
/// Windows进程辅助类
///
public class ProcessHelper
{
///
/// 判断是否已经存在运行的实例
///
/// 存在返回true,不存在返回false
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;
}
}
///
/// 查询指定名称的进程是否存在
///
/// 要查找的进程名称
/// 存在返回true,不存在返回false
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;
}
}
///
/// 检测端口是否被占用
///
/// 要检测的端口号
/// 占用返回true,未被占用则返回false
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;
}
}
}