当程序打开后 监测进程是否在执行,如果没有执行将自动打开启动路径对应的文件,详见下图
using System;using System.Diagnostics;using System.Drawing;using System.IO;using System.Linq;using System.Net;using System.Net.Sockets;using System.Text;using System.Threading;using System.Windows.Forms;namespace 进程监测及自动执行{ public partial class Main : Form { public string t1, t2; public int i = 0; public Main() { InitializeComponent(); t1 = System.Configuration.ConfigurationManager.AppSettings["ProcessName"]; t2 = System.Configuration.ConfigurationManager.AppSettings["ProcessPath"]; textBox1.Text = t1; textBox2.Text = t2; } private void timer1_Tick(object sender, EventArgs e) { if (Process.GetProcessesByName(t1).ToList().Count > 0) { //存在 } else { Process mProcess = new Process(); mProcess.StartInfo.FileName = t2; mProcess.StartInfo.WorkingDirectory = t2.Substring(0, t2.LastIndexOf("\\")); mProcess.Start(); } } } }
评论