using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
namespace ProcessMonitoring
{
public partial class Form1 : Form
{
string filePath = System.Environment.CurrentDirectory + @"\data.txt";
string logPath = System.Environment.CurrentDirectory + @"\log.txt";
ProcessMonitoring pm = new ProcessMonitoring();
Form2 f2 = new Form2();
bool flag = false;//检测开关
Timer timer = new Timer();
public static List<string> lists;//要检测的值集合
public string comboBoxVal;
//
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
}
private void Form1_Load(object sender, EventArgs e)
{
var f2 = new Form2();
//创建文件
//获取当前程序运行的目录
pm.createFile(filePath);
pm.createFile(logPath);
//创建定时器
timer.Interval = 1000 * Form2.interval_Time;
timer.Tick += new EventHandler(start);
setCheckedListBox();
getListBoxValue();
//下拉框设置
this.comboBox1.SelectedIndex = 0;
this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
//checkedListBox1设置
this.checkedListBox1.CheckOnClick = true;
}
/// <summary>
/// setCheckedListBox设置值
///// </summary>
public void setCheckedListBox()
{
List<string> list = pm.readFile(filePath);
this.checkedListBox1.Items.Clear();
this.comboBox1.Items.Clear();
foreach (string item in list)
{
// MessageBox.Show(item);
this.checkedListBox1.Items.Add(pm.getProcessName(item));
this.comboBox1.Items.Add(pm.getProcessName(item));
}
// MessageBox.Show(this.checkedListBox1.Items.Count.ToString());
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemChecked(i, true);
}
}
/// <summary>
/// 获取选择的值
/// </summary>
public void getListBoxValue()
{
List<string> list = new List<string>();
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetItemChecked(i))//先判断是否被选中
{
list.Add(checkedListBox1.GetItemText(checkedListBox1.Items[i])) ;
}
}
lists = list;
}
//创建间隔时间
public void start(object sender, EventArgs e)
{
appRun(flag);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
f2.ShowDialog();
}
private void appRun(bool flag)
{
if (flag)
{
foreach (string line in lists)
{
if (line != "")
{
String message = pm.findProcess(line);
if (message != "")
{
//追加message信息到log日志
string s = File.ReadAllText(logPath).Insert(0, message + "\r\n");
File.WriteAllText(logPath,s);
}
}
}
queryData(logPath);
}
}
private void button2_Click(object sender, EventArgs e)
{
// MessageBox.Show(Form2.interval_Time.ToString());
if (button2.Text=="开始检测")
{
timer.Start();
timer.Interval = 1000 * Form2.interval_Time;
button2.Text = "检测中...";
flag = true;
button2.BackColor = Color.Green;
}else
{
timer.Stop();
button2.Text = "开始检测";
flag = false;
button2.BackColor = Color.White;
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void queryData(string filePath)
{
string[] lines = File.ReadAllLines(filePath);
this.listBox1.Items.Clear();
foreach (string line in lines)
{
if (line != "" && this.listBox1.Items.Count <= 20)
{
this.listBox1.Items.Insert(this.listBox1.Items.Count, line);
}
}
this.listBox1.MultiColumn = true;
}
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
getListBoxValue();
}
private void refush_Click(object sender, EventArgs e)
{
setCheckedListBox();
}
private void button3_Click(object sender, EventArgs e)
{
this.listBox1.Items.Clear();
}
private void button4_Click(object sender, EventArgs e)
{
/*
foreach (var list in lists)
{
foreach (var item in pm.readFile(filePath))
{
if (list == pm.getProcessName(item))
{
pm.startProcess(pm.getProcessName(item));
}
}
}
*/
pm.startProcess(this.comboBox1.Text);
}
private void button5_Click(object sender, EventArgs e)
{
try
{
pm.startProcess(logPath);
}
catch (Exception a)
{
MessageBox.Show(a.Message.ToString());
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// MessageBox.Show(this.comboBox1.SelectedValue.ToString());
}
private void button6_Click(object sender, EventArgs e)
{
pm.killProcess(this.comboBox1.Text);
pm.startProcess(this.comboBox1.Text);
}
}
}