using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace KillGames
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
//string str = System.Environment.CurrentDirectory;
////存储进程的文件路径
//string path = str + @"\ProcessList.txt";
//string bat = str + @"\look.bat";
string strPath = @"D:\KillGame\ProcessList.txt";
FileStream fs = new FileStream(@"d:\xx.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
string[] ProcessNames = File.ReadAllLines(strPath);
sw.WriteLine( DateTime.Now.ToString()+"\n");
sw.Flush();
sw.Close();
fs.Close();
KillProcess();
}
public static void KillProcess()
{
//string str = System.Windows.Forms.Application.StartupPath;
//当前程序运行文件目录
//存储进程名的文件
string strPath = @"D:\KillGame\ProcessList.txt";
string strBat = @"D:\KillGame\look.bat";
//存储进程的文件路径
//string path = str + @"\ProcessList.txt";
//string bat = str + @"\look.bat";
if (!File.Exists(strPath))
{
File.Create(strPath);
}
//逐行读取文件的进程名
string[] ProcessNames = File.ReadAllLines(strPath);
if (ProcessNames.Length > 0)
{
for (int i = 0; i < ProcessNames.Length; i++)
{
//当前运行的所有进程
Process[] temp = Process.GetProcesses();
//rocessNames[i] = ProcessNames[i].Substring(0, ProcessNames[i].Length - 4);
foreach (Process vProcess in temp)
{
if (vProcess.ProcessName == ProcessNames[i])
{
vProcess.Kill();
//ProcessStartInfo psi = new ProcessStartInfo(strBat);
//Process p = new Process();
//p.StartInfo = psi;
//p.Start();
//Process.Start("cmd.exe");
}
}
}
}
Thread.Sleep(10000);
KillProcess();
}
protected override void OnStop()
{
FileStream fs = new FileStream(@"d:\xx.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine(DateTime.Now.ToString() + "\n");
sw.Flush();
sw.Close();
fs.Close();
}
}
}