//获取配置文件中的需要监控项
private static string MonitorServe = ConfigurationSettings.AppSettings["MonitorServe"];
static void Main(string[] args)
{
StartMonitor();
}
/// <summary>
/// 开启监控
/// </summary>
private static void StartMonitor()
{
int ProceedingCount = 0;
string[] strMonitor = MonitorServe.Split('$');
for (int i = 0; i < strMonitor.Length; i++)
{
if (!string.IsNullOrEmpty(strMonitor[i]))
{
Process[] Processes;
Processes = Process.GetProcessesByName(strMonitor[i]);
foreach (Process IsProcedding in Processes)
{
if (IsProcedding.ProcessName == strMonitor[i])
{ ProceedingCount += 1; }
}
if (ProceedingCount < 1)
{
Console.WriteLine("服务已开启");
}
//关闭重复启动项
if (ProceedingCount > 1)
{
foreach (Process myProcess in Processes)
{
myProcess.Kill();
}
}
}
}