找传奇、传世资源到传世资源站!

C# 进程守护 示例源码

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

from clipboardusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Diagnostics;using System.Xml;using System.IO;using Microsoft.Win32;namespace ProcessMonitor{ public partial class frmMain : Form { private List<MyProcess> processes=null; private int count = 0; private int xhcount = 0; string pathcs = ""; public frmMain() { InitializeComponent(); this.SizeChanged = new System.EventHandler(this.Form1_SizeChanged); } private void btnGetProcess_Click(object sender, EventArgs e) { MyHelper.AddLog("开始守护..."); processes = MyHelper.LoadMonitorProcesses(); textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " "开始守护..." "\r\n"); timer1.Enabled = true; btnGetProcess.Enabled = false; btnStop.Enabled = true; startMenuItem1.Enabled = false; stopMenuItem1.Enabled = true; tsslStatus.Text = "守护中..."; } private void timer1_Tick(object sender, EventArgs e) { // textBox1.GetLineFromCharIndex(textBox1.TextLength) 1 //获取没换行的行数 if (textBox1.Lines.Length > 30 ) { textBox1.Text = ""; //自动清空接收区,行数不限制太快了。 } xhcount ; label3.Text = Convert.ToString(xhcount); MyHelper.AddLog("进程守护已经运行了" xhcount "个循环周期"); ReLoadListBox(); if (processes!=null) { foreach (var process in processes) { if (!process.Multi) { Process[] processList; processList = System.Diagnostics.Process.GetProcessesByName(process.Name); if (processList != null && processList.Count() > 1) { textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " process.Name "有" processList.Count() "个进程,将逐一关闭后只保留一个" "\r\n");int i = processList.Count(); foreach (Process p in processList) { do { p.Kill(); // 杀进程,调试有错,全部退出了,只能加break,一个循环周期杀一个。 i--; //System.Threading.Thread.Sleep(1000); } while ( i > 1 ); //只保留一个进程 //Processlist.Items.Remove(process.Name); //continue; //会导致程序全部退出,break不会,但是每个周期只杀一个。 break; //continue;结束本次循环 } } else { if (processList != null && processList.Count() != 1) //单开程序没有运行的情况 { //单开程序首次运行 //MessageBox.Show(process.Name "程序未找到尝试重新启动"); //textBox1.AppendText(process.Name "单开程序未找到,尝试首次启动" "\r\n"); try { //MyHelper.AddLog("进程<" process.Name ">未找到,尝试打开<" process.Path ">."); if (process.Path.Contains(",")) //查找路径中是否有",",有就返回true { int fg = process.Path.IndexOf(","); //找到","所在位置的索引 string left = process.Path.Substring(0, fg); //将","左边的字符串赋给地址 pathcs = process.Path.Substring(fg 1); //将","右边的字符串给启动参数 System.Diagnostics.Process.Start(left,pathcs); //带参数运行程序 textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " process.Name "单开带参数" pathcs "首次运行成功" "\r\n"); count ; lblCount.Text = count.ToString(); //MyHelper.AddLog("首次启动<" process.Path ">成功."); } else { System.Diagnostics.Process.Start(process.Path); //当前路径直接完成程序名 count ; lblCount.Text = count.ToString(); textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " process.Name "单开首次运行成功" "\r\n"); //MyHelper.AddLog("首次启动<" process.Path ">成功."); } } catch (Exception exp) { stop(); System.Windows.Forms.MessageBox.Show("进程守护停止运行:" exp.Message "\r\nPath:" process.Path); } } else { //单开程序已经运行,不用判断也可以。 if (processList.Length > 0) //大于0检测到进程,说明程序在运行,这步可以取消。 { textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " process.Name "单开正在运行,被守护中..." "\r\n"); //textBox1.Text = "找到" processList.Length "个进程" "\r\n"; } } } } if (process.Multi) //判断是否多开 { Process[] processList; processList = System.Diagnostics.Process.GetProcessesByName(process.Name); if (processList != null && process.linecount < 100) //配置文件的进程数大于1,小于100的情况 { if (processList != null && processList.Count() < process.linecount) //比较当前进程小于设置值的情况 { if (processList.Count() == 0 || processList.Length < 1 ) //Processlist.Items.Contains(process.Name) { //程序没有运行,只判断其中一个就可以,进程名有大小写区分所以去掉了 try { if (process.Path.Contains(",")) //查找路径中是否有",",有就返回true { int fg = process.Path.IndexOf(","); //找到","所在位置的索引 string left = process.Path.Substring(0, fg); //将","左边的字符串赋给地址 pathcs = process.Path.Substring(fg 1); //将","右边的字符串给启动参数 System.Diagnostics.Process.Start(left, pathcs); //带参数运行程序 textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " process.Name "多开带参数" pathcs "首次运行成功" "\r\n"); count ; lblCount.Text = count.ToString(); //MyHelper.AddLog("首次启动<" process.Path ">成功."); } else { //MyHelper.AddLog("进程<" process.Name ">尝试多开<" process.Path ">."); System.Diagnostics.Process.Start(process.Path); count ; lblCount.Text = count.ToString(); textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " process.Name "多开首次运行成功" "\r\n"); //MyHelper.AddLog("进程<" process.Path ">多开成功" count.ToString() "次"); } } catch (Exception exp) { stop(); System.Windows.Forms.MessageBox.Show("进程守护停止运行:" exp.Message "\r\nPath:" process.Path); } } else { //进程已有并小于设置限制数,可能出现限制数和启动数不一致的情况(开了多个进程在运行本守护程序) try { if (process.Path.Contains(",")) //查找路径中是否有",",有就返回true { int fg = process.Path.IndexOf(","); //找到","所在位置的索引 string left = process.Path.Substring(0, fg); //将","左边的字符串赋给地址 pathcs = process.Path.Substring(fg 1); //将","右边的字符串给启动参数 System.Diagnostics.Process.Start(left, pathcs); //带参数运行程序 textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " process.Name "多开带启动参数" pathcs "运行" "\r\n"); count ; lblCount.Text = count.ToString(); //MyHelper.AddLog("首次启动<" process.Path ">成功."); } else { //MyHelper.AddLog("进程<" process.Name ">尝试多开<" process.Path ">."); System.Diagnostics.Process.Start(process.Path); count ; lblCount.Text = count.ToString(); textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " process.Name "多开运行" "\r\n"); //MyHelper.AddLog("进程<" process.Path ">多开成功" count.ToString() "次"); //刷新当前线程,解决提示要下一个周期才显示的问题 processList = System.Diagnostics.Process.GetProcessesByName(process.Name); if (processList.Count() == process.linecount) { textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " process.Name "首次运行到多开的最大" process.linecount "个进程数限制,被守护中..." "\r\n"); } } } catch (Exception exp) { stop(); System.Windows.Forms.MessageBox.Show("进程守护停止运行:" exp.Message "\r\nPath:" process.Path); } } //多开程序小于99并小于限制数的情况结束 } if (processList != null && processList.Count() > process.linecount) { textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " process.Name "进程数为" processList.Count() "个," "超过最大" process.linecount "个限制,将逐一杀掉多余的进程,只保留" process.linecount "个" "\r\n"); //进程数大于等于5的情况,主要是初次启动时候检测到的 int i = processList.Count(); foreach (Process p in processList) { do { p.Kill(); //杀进程全部 i--; } while (i > process.linecount); //保留限制线程数 Processlist.Items.Remove(process.Name); break; // 每个循环周期结束一个 //continue; //结束本次循环,会导致全部进程退出 }} //初次启动的时候,已经开了这么多提示。 if (processList.Count() == process.linecount) { textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " process.Name "已达多开的最大" process.linecount "个进程数限制,被守护中..." "\r\n"); } } //多开线程数小于100的情况结束 if (processList != null && process.linecount == 100) //无限制模式运行 { if (processList.Count() == 0 || processList.Length < 1) //|| !Processlist.Items.Contains(process.Name) { //程序没有运行的情况,上面3种情况任意一种成立都可以判断运行没有运行,判断进程名要区分大小写。 try { if (process.Path.Contains(",")) //查找路径中是否有",",有就返回true { int fg = process.Path.IndexOf(","); //找到","所在位置的索引 string left = process.Path.Substring(0, fg); //将","左边的字符串赋给地址 pathcs = process.Path.Substring(fg 1); //将","右边的字符串给启动参数 System.Diagnostics.Process.Start(left, pathcs); //带参数运行程序 textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " process.Name "进入无限制模式带启动参数" pathcs "首次运行" "\r\n"); count ; lblCount.Text = count.ToString(); //MyHelper.AddLog("首次启动<" process.Path ">成功."); } else { //MyHelper.AddLog("进程<" process.Name ">尝试多开<" process.Path ">."); System.Diagnostics.Process.Start(process.Path); count ; lblCount.Text = count.ToString(); textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " process.Name "进入无限制模式首次运行" "\r\n"); //MyHelper.AddLog("进程<" process.Path ">多开成功" count.ToString() "次"); } } catch (Exception exp) { stop(); System.Windows.Forms.MessageBox.Show("进程守护停止运行:" exp.Message "\r\nPath:" process.Path); } } //无限制程序,没有启动情况结束 else { textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " process.Name "已进入无限制模式,检测到" processList.Count() "个进程,防退出守护中..." "\r\n"); } } //无限制模式运行程序结束 } // 多开结束处} } else { stop(); System.Windows.Forms.MessageBox.Show("没有找到需要守护的程序,请检查配置文件Processes.xml是否正确.\r\n[相关的错误信息请见EventLog目录下的日志.]"); } } private void ReLoadListBox() { Processlist.Items.Clear(); Process[] processes; processes = System.Diagnostics.Process.GetProcesses(); Process process; for (int i = 0; i < processes.Length - 1; i ) { process = processes[i]; Processlist.Items.Add(process.ProcessName); } } private void btnStop_Click(object sender, EventArgs e) { stop(); } private void lbProcess_DoubleClick(object sender, EventArgs e) { try { string s = ""; Process[] processes; //Retrieve the additional information about a specific process processes = System.Diagnostics.Process.GetProcessesByName(Processlist.SelectedItem.ToString()); for (int i=0; i < processes.Count(); i ) { Process process = processes[i]; s = s "----" process.Id ":" process .ProcessName "----" "\r\n"; s = s "进程ID:" Convert.ToString(process.Id) " \r\n"; s = s "进程名称:" Convert.ToString(process.ProcessName) " \r\n"; s = s "该进程的总体优先级类别:" Convert.ToString(process.PriorityClass) " \r\n"; s = s "由该进程打开的句柄数:" process.HandleCount "\r\n"; s = s "该进程的主窗口标题:" process.MainWindowTitle "\r\n"; s = s "该进程允许的最小工作集大小:" process.MinWorkingSet.ToString() " \r\n"; s = s "该进程允许的最大工作集大小:" process.MaxWorkingSet.ToString() " \r\n"; s = s "该进程的分页内存大小:" process.PagedMemorySize64 "\r\n"; s = s "该进程的峰值分页内存大小:" process.PeakPagedMemorySize64 "\r\n"; s = s "----end----" "\r\n"; } System.Windows.Forms.MessageBox.Show(s); } catch { System.Windows.Forms.MessageBox.Show("不是合法的进程ID!"); } } private void stop() { timer1.Enabled = false; btnGetProcess.Enabled = true; btnStop.Enabled = false; startMenuItem1.Enabled = true; stopMenuItem1.Enabled = false; tsslStatus.Text = "停止"; textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " "停止守护..." "\r\n"); MyHelper.AddLog("停止守护..."); } private void Form1_SizeChanged(object sender, EventArgs e) { if (this.WindowState==FormWindowState.Minimized) { this.Hide(); this.notifyIcon1.Visible=true; } } private void frmMain_Load(object sender, EventArgs e) { //System.Environment.CurrentDirectory = Application.StartupPath; //3个都可以,CurrentDirectory加入注册表自启重启后会变为system32 //System.Environment.CurrentDirectory = System.Windows.Forms.Application.StartupPath; System.Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory; //btnGetProcess.PerformClick(); //启动后自动开始只支持win7 ,win10无效 MyHelper.AddLog("开始守护..."); processes = MyHelper.LoadMonitorProcesses(); textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " "开始守护..." "\r\n"); btnGetProcess.Enabled = false; btnStop.Enabled = true; startMenuItem1.Enabled = false; stopMenuItem1.Enabled = true; tsslStatus.Text = "守护中..."; textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " "正在读取配置..." "\r\n"); int a = 0, b = 0; processes = MyHelper.LoadMonitorProcesses(); // 重复装载是避免第一次初始化配置出错自动修正后,不能立即生效的问题 foreach (var process in processes) { if (process.Multi == true && process.linecount <= 100) //第一次读配置已经修正,linecount大于1,小于等于100 { if (process.linecount >= a ) { a = process.linecount; } } else { textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " process.Name "程序准备单开运行" "\r\n"); //textBox2.Text 显示默认值1. } if (process.jgtime >= b && process.jgtime >= 500) //不用判断是否大于500,初始化配置时已经修正。 { //textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " process.Name "间隔时间非默认配置,将使用配置文件中最大循环时间" "\r\n"); if ( process.jgtime >= b ) { b = process.jgtime; } } //else //{ // textBox3.Text = Convert.ToString(process.jgtime); // textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " process.Name "间隔时间启用默认值23秒" "\r\n"); //} } if (a > 1) //单开textBox2.Text显示默认值 { if (a==100) { textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " "配置中有程序设置了无限制模式运行" "\r\n"); } else { textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " "配置中有多开程序,最大线程数为" a "个" "\r\n"); } textBox2.Text = Convert.ToString(a); // 将配置文件中线程最大的值显示出来,1表示单开 ,100表示无限制 } //if (b >= 500) //毫秒限制,小于500,则显示默认的23000,/不用判断是否大于500,初始化配置时已经修正。 //{ timer1.Interval = b; textBox3.Text = Convert.ToString(b); //读取配置文件中的间隔时间 textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " "配置中最大循环启动时间为" b "毫秒" "\r\n"); //} //else //{ // textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " "间隔时间配置错误,将以默认的" timer1.Interval "毫秒运行,并保存到配置文件" "\r\n"); // //button3.PerformClick(); //将默认间隔时间保存到配置文件 //} if (IsRegistryValueNameExist("ProcessMonitor")) // 读取注册表开机启动配置 { checkBox1.Checked = true; } //else //{ // checkBox1.Checked = false; //} timer1.Enabled = true; //循环周期开始计时 textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " "配置读取完毕!" "\r\n"); } private void MenuItem2_Click(object sender, EventArgs e) { if (MessageBox.Show("你确定要退出程序吗?", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK) { notifyIcon1.Visible = false; this.Close(); this.Dispose(); Application.Exit(); } } private void MenuItem1_Click(object sender, EventArgs e) { this.Show(); this.WindowState = FormWindowState.Normal; this.Activate(); } private void notifyIcon1_DoubleClick(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Normal) { this.WindowState = FormWindowState.Minimized; this.Hide(); } else if (this.WindowState == FormWindowState.Minimized) { this.Show(); this.WindowState = FormWindowState.Normal; this.Activate(); } } private void frmMain_FormClosing(object sender, FormClosingEventArgs e) { this.Hide(); e.Cancel = true; } private void stopMenuItem1_Click(object sender, EventArgs e) { stop(); } private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { if (MessageBox.Show("你确定要退出程序吗?", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK) { notifyIcon1.Visible = false; this.Close(); this.Dispose(); Application.Exit(); } } private void startMenuItem1_Click(object sender, EventArgs e) { btnGetProcess.PerformClick(); } private void button1_Click(object sender, EventArgs e) { //if (int.TryParse(textBox2.Text, out linecount) == true) //无法解决负数的问题 //{ // if (int.Parse(textBox2.Text) > 1) // { // linecount = int.Parse(textBox2.Text); // MessageBox.Show("设置成功"); //线程数记入内存,未保存。 // } //下面的方式输入格式出错暂时不知怎么处理 //if (int.Parse(textBox2.Text) > 1) //{ // linecount = int.Parse(textBox2.Text); // MessageBox.Show("设置成功"); //线程数记入内存,未保存。 if (int.Parse(textBox2.Text) > 1 && int.Parse(textBox2.Text) < 100 ) //多开程序的最大线程99 { foreach (var process in processes) { if (process.Multi) { if (System.IO.File.Exists("processes.xml")) //可以不用判断文件是否存在,运行了就有 { //process.linecount = int.Parse(textBox2.Text); //textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " process.Name "的线程数" textBox2.Text "设置成功" "\r\n"); //修改配置文件 XmlDocument dom = new XmlDocument(); dom.Load("processes.xml"); XmlNode root = dom.SelectSingleNode("monitor"); //要修改的根节点, XmlNodeList root1 = root.ChildNodes; //列出monitor所有子节点 foreach (XmlNode root2 in root1) //遍厉所有子节点, 有多少个程序,就会写入多少次配置。 { XmlElement xe = (XmlElement)root2; //将节点转换一下类型 //判断该子节点下的值为非空并且mult的值为true才有效。 //if ((xe.GetAttribute("linecount") != null) && (xe.GetAttribute("mutl")="false") //if (xe.GetAttribute("linecount") != null || xe.GetAttribute("linecount") != "" ) //之前读配置的时候已经修复了错误的情况,这也可以不用判断 //{ if (xe.GetAttribute("linecount") == "100" ) //linecount设置为100的程序不允许在界面上修改 { //process.linecount = 100; //MessageBox.Show(process.Name "配置为无限制运行模式,线程数不允许修改,仍是100"); MyHelper.AddLog(process.Name "配置为无限制运行模式,线程数不允许修改,仍是100"); //break; //processList = System.Diagnostics.Process.GetProcessesByName(process.Name); } else //设置不等于100的情况 { xe.SetAttribute("linecount", textBox2.Text); //设置新值 if (process.linecount != 100) //process.linecount为100不允许更新当前线程限制数 { process.linecount = int.Parse(textBox2.Text); //修改配置后,更新当前使用的限制数 MyHelper.AddLog("修改并设置" process.Name "的线程数" textBox2.Text "成功"); //textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " "修改并设置" process.Name "的线程数" textBox2.Text "成功" "\r\n"); } } //} //else //{ // xe.SetAttribute("linecount", textBox2.Text); // process.linecount = int.Parse(textBox2.Text); // textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " "修改并设置" process.Name "的线程数" textBox2.Text "成功" "\r\n"); // //textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " "添加" process.Name "的线程数" textBox2.Text "成功" "\r\n"); //} //XmlNode linecount = root2.SelectSingleNode("linecount"); //linecount.InnerText = textBox2.Text; // 将textBox2.Text写入xml文件 //break; } dom.Save("processes.xml"); if (process.linecount != 100 ) { textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " process.Name "的线程数" textBox2.Text "保存到配置文件成功" "\r\n"); MessageBox.Show("修改" process.Name "的线程数为" textBox2.Text "成功" "\r\n"); } else { textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " process.Name "配置为无限制运行模式,线程数不允许修改,仍是100" "\r\n"); MessageBox.Show(process.Name "配置为无限制运行模式,线程数不允许修改,仍是100"); } } else { //MyHelper.AddLog("配置文件processes.xml不存在"); MessageBox.Show("配置文件processes.xml不存在"); } } else { textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " process.Name "没有启用多开" "\r\n"); MessageBox.Show(process.Name "没有启用多开,请修改后再试"); } } } else { MessageBox.Show("请重新输入大于1并小于100的整数"); } } private void textBox2_KeyPress(object sender, KeyPressEventArgs e) //只允许输入数字及删除键 { { e.Handled = true; if ((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == (char)8) { e.Handled = false; } } } private void textBox2_Click(object sender, EventArgs e) { textBox2.Text = ""; textBox2.ForeColor = Color.Red; textBox2.ReadOnly = false; } private void button2_Click(object sender, EventArgs e) { textBox1.Text = ""; } private void button3_Click(object sender, EventArgs e) { if (int.Parse(textBox3.Text) >= 500 && int.Parse(textBox3.Text) <= 1000000000) //时间最小500毫秒,最大1000000000毫秒 { foreach (var process in processes) { //if (System.IO.File.Exists("processes.xml")) //{ //process.jgtime = int.Parse(textBox3.Text); // 启动读取时候用 timer1.Interval = int.Parse(textBox3.Text); //直接赋值 textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " process.Name "的循环启动时间" textBox3.Text "毫秒设置成功" "\r\n"); XmlDocument dom = new XmlDocument(); dom.Load("processes.xml"); XmlNode root = dom.SelectSingleNode("monitor"); //要修改的根节点 XmlNodeList root1 = root.ChildNodes; //列出所有子节点 foreach (XmlNode root2 in root1) //遍厉所有子节点 { XmlElement xe = (XmlElement)root2; //将节点转换一下类型 //判断该子节点下的值为非空并且mult的值为true才有效。 //if ((xe.GetAttribute("linecount") != null) && (xe.GetAttribute("mutl")="false") if (xe.GetAttribute("jgtime") != null) //空值的情况暂时不好处理 { xe.SetAttribute("jgtime", textBox3.Text); //设置新值 //textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " "修改" process.Name "的循环时间为" textBox3.Text "毫秒成功" "\r\n"); } else { xe.SetAttribute("jgtime", textBox3.Text); //textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " "添加" process.Name "的循环时间为" textBox3.Text "毫秒成功" "\r\n"); } //break; //为了测试 } dom.Save("processes.xml"); textBox1.AppendText(DateTime.Now.ToLongTimeString().ToString() " " process.Name "的循环启动时间" textBox3.Text "毫秒保存到配置文件成功" "\r\n"); MessageBox.Show("修改" process.Name "的循环启动时间为" textBox3.Text "毫秒"); //} //else //{ // //MyHelper.AddLog("配置文件processes.xml不存在"); // MessageBox.Show("配置文件processes.xml不存在"); //} } } else { MessageBox.Show("请重新输入小于等于500大于等于1000000000的整数,单位为毫秒"); } } private void textBox1_TextChanged(object sender, EventArgs e) { } private void textBox3_TextChanged(object sender, EventArgs e) { } private void textBox3_KeyPress(object sender, KeyPressEventArgs e) { { e.Handled = true; if ((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == (char)8) { e.Handled = false; } } } private void textBox3_Click(object sender, EventArgs e) { textBox3.Text = ""; textBox3.ForeColor = Color.Red; textBox3.ReadOnly = false; } private void textBox3_MouseHover(object sender, EventArgs e) { //ToolTip toolTip1 = new ToolTip(); //// 设置显示样式 //toolTip1.AutoPopDelay = 5000;//提示信息的可见时间 //toolTip1.InitialDelay = 500;//事件触发多久后出现提示 //toolTip1.ReshowDelay = 500;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框 //toolTip1.ShowAlways = true;//是否显示提示框 //// 设置伴随的对象. //toolTip1.SetToolTip(this.textBox3, "循环时间,所有程序一个时间,最小500毫秒,最大1000000000毫秒"); ToolTip ttpSettings = new ToolTip(); ttpSettings.InitialDelay = 200; ttpSettings.AutoPopDelay = 10 * 1000; ttpSettings.ReshowDelay = 200; ttpSettings.ShowAlways = true; ttpSettings.IsBalloon = true; string tipOverwrite = "所有程序的循环启动时间,最小500毫秒,最大1000000000毫秒"; ttpSettings.SetToolTip(this.textBox3, tipOverwrite); } private void textBox2_MouseHover(object sender, EventArgs e) { //ToolTip toolTip1 = new ToolTip(); //toolTip1.AutoPopDelay = 5000;//提示信息的可见时间 //toolTip1.InitialDelay = 500;//事件触发多久后出现提示 //toolTip1.ReshowDelay = 500;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框 //toolTip1.ShowAlways = true;//是否显示提示框 //toolTip1.SetToolTip(this.textBox2, "启用多开后设置才有效,保存后同步所有程序,可修改配置文件为不同的线程数,大于1小于等于99999的整数"); ToolTip ttpSettings = new ToolTip(); ttpSettings.InitialDelay = 200; ttpSettings.AutoPopDelay = 10 * 1000; ttpSettings.ReshowDelay = 200; ttpSettings.ShowAlways = true; ttpSettings.IsBalloon = true; string tipOverwrite = "不同程序线程数可设不同,1为单开运行,100为无限制模式,默认显示配置中最大的数"; ttpSettings.SetToolTip(this.textBox2, tipOverwrite); } private void button2_MouseHover(object sender, EventArgs e) { //ToolTip toolTip1 = new ToolTip(); //toolTip1.AutoPopDelay = 5000;//提示信息的可见时间 //toolTip1.InitialDelay = 500;//事件触发多久后出现提示 //toolTip1.ReshowDelay = 500;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框 //toolTip1.ShowAlways = true;//是否显示提示框 //toolTip1.SetToolTip(this.button2, "清空日志接收区"); ToolTip ttpSettings = new ToolTip(); ttpSettings.InitialDelay = 500; ttpSettings.AutoPopDelay = 5000; ttpSettings.ReshowDelay = 500; ttpSettings.ShowAlways = true; ttpSettings.IsBalloon = true; string tipOverwrite = "清空日志接收区,满30行自动清空"; ttpSettings.SetToolTip(this.button2, tipOverwrite); } private void button3_MouseHover(object sender, EventArgs e) { //ToolTip toolTip1 = new ToolTip(); //toolTip1.AutoPopDelay = 5000;//提示信息的可见时间 //toolTip1.InitialDelay = 500;//事件触发多久后出现提示 //toolTip1.ReshowDelay = 500;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框 //toolTip1.ShowAlways = true;//是否显示提示框 //toolTip1.SetToolTip(this.button3, "保存循环时间到配置文件,并更新当然的设置"); ToolTip ttpSettings = new ToolTip(); ttpSettings.InitialDelay = 200; ttpSettings.AutoPopDelay = 10 * 1000; ttpSettings.ReshowDelay = 200; ttpSettings.ShowAlways = true; ttpSettings.IsBalloon = true; string tipOverwrite = "保存循环启动时间到配置文件Processes.xml,并更新当然的设置"; ttpSettings.SetToolTip(this.button3, tipOverwrite); } private void button1_MouseHover(object sender, EventArgs e) { //ToolTip toolTip1 = new ToolTip(); //toolTip1.AutoPopDelay = 5000;//提示信息的可见时间 //toolTip1.InitialDelay = 500;//事件触发多久后出现提示 //toolTip1.ReshowDelay = 500;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框 //toolTip1.ShowAlways = true;//是否显示提示框 //toolTip1.SetToolTip(this.button1, "保存线程数到配置文件,并更新当然的设置"); ToolTip ttpSettings = new ToolTip(); ttpSettings.InitialDelay = 200; ttpSettings.AutoPopDelay = 10 * 1000; ttpSettings.ReshowDelay = 200; ttpSettings.ShowAlways = true; ttpSettings.IsBalloon = true; string tipOverwrite = "程序多开时才有效,保存线程数到配置文件Processes.xml,并更新当然的设置"; ttpSettings.SetToolTip(this.button1, tipOverwrite); } private void lblCount_MouseHover(object sender, EventArgs e) { //ToolTip toolTip1 = new ToolTip(); //toolTip1.AutoPopDelay = 5000;//提示信息的可见时间 //toolTip1.InitialDelay = 500;//事件触发多久后出现提示 //toolTip1.ReshowDelay = 500;//指针从一个控件移向另一个控件时,经过多久才会显示下一个提示框 //toolTip1.ShowAlways = true;//是否显示提示框 //toolTip1.SetToolTip(this.lblCount, "所有程序的累计运行次数,可完整显示最多6位数"); ToolTip ttpSettings = new ToolTip(); ttpSettings.InitialDelay = 200; ttpSettings.AutoPopDelay = 10 * 1000; ttpSettings.ReshowDelay = 200; ttpSettings.ShowAlways = true; ttpSettings.IsBalloon = true; string tipOverwrite = "所有程序的累计启动次数,可完整显示最多6位数"; ttpSettings.SetToolTip(this.lblCount, tipOverwrite); } private void checkBox1_CheckedChanged(object sender, EventArgs e) //设置开机启动项 { if (checkBox1.Checked == true) { AutoStart(true); //MessageBox.Show("开机启动设置成功"); } else { AutoStart(false); //MessageBox.Show("取消开机启动成功"); } } // <summary> /// 修改程序在注册表中的键值 /// </summary> /// <param name="isAuto">true:开机启动,false:不开机自启</param> public static void AutoStart(bool isAuto) //修改开机启动项注册表键值 { try { if (isAuto == true) { RegistryKey R_local = Registry.CurrentUser; //设置的是当前用户有效,如果是所有用户改成LocalMachine RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); R_run.SetValue("ProcessMonitor", Application.ExecutablePath); R_run.Close(); R_local.Close(); } else { RegistryKey R_local = Registry.CurrentUser;//RegistryKey R_local = Registry.CurrentUser; RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); R_run.DeleteValue("ProcessMonitor", false); R_run.Close(); R_local.Close(); } //GlobalVariant.Instance.UserConfig.AutoStart = isAuto; } catch (Exception) { //MessageBoxDlg dlg = new MessageBoxDlg(); //dlg.InitialData("您需要管理员权限修改", "提示", MessageBoxButtons.OK, MessageBoxDlgIcon.Error); //dlg.ShowDialog(); MessageBox.Show("您需要管理员权限修改", "提示"); } } //private bool IsRegistryKeyExist(string sKeyName) //读取开机启动项是否有ProcessMonitor键 //{ // string[] sKeyNameColl; // RegistryKey hklm = Registry.LocalMachine; //所有用户 // RegistryKey hkSoftWare = hklm.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); // sKeyNameColl = hkSoftWare.GetSubKeyNames(); //获取SOFTWARE下所有的子项 // foreach (string sName in sKeyNameColl) // { // if (sName == sKeyName) // { // hklm.Close(); // hkSoftWare.Close(); // return true; // } // } // hklm.Close(); // hkSoftWare.Close(); // return false; //} private bool IsRegistryValueNameExist(string ProcessMonitor) //读取开机启动项是否有ProcessMonitor值 { string[] sValueNameColl; RegistryKey hkcu = Registry.CurrentUser; //当前用户下 RegistryKey hkrun = hkcu.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); sValueNameColl = hkrun.GetValueNames(); //获取run下所有键值的名称 foreach (string sName in sValueNameColl) { if (sName == ProcessMonitor) { hkcu.Close(); hkrun.Close(); return true; } } hkcu.Close(); hkrun.Close(); return false; } private void checkBox1_MouseHover(object sender, EventArgs e) { ToolTip ttpSettings = new ToolTip(); ttpSettings.InitialDelay = 200; ttpSettings.AutoPopDelay = 10 * 1000; ttpSettings.ReshowDelay = 200; ttpSettings.ShowAlways = true; ttpSettings.IsBalloon = true; string tipOverwrite = "只对当前用户有效,支持带参数运行,参数加在配置文件中路径后面,用英文逗号隔开"; ttpSettings.SetToolTip(checkBox1, tipOverwrite); // checkBox1 is a checkbox } private void label3_MouseHover(object sender, EventArgs e) { ToolTip ttpSettings = new ToolTip(); ttpSettings.InitialDelay = 200; ttpSettings.AutoPopDelay = 10 * 1000; ttpSettings.ReshowDelay = 200; ttpSettings.ShowAlways = true; ttpSettings.IsBalloon = true; string tipOverwrite = "累计的守护周期次数,最大可显示出13位数"; ttpSettings.SetToolTip(this.label3, tipOverwrite); } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复