Process[] myprocess = Process.GetProcessesByName("a");
if (myprocess.Count() > 0)//判斷如果存在
{
//myprocess[0].Kill();//關閉程序
}
else
{
try
{
Process newProcess = new Process();//創建一個新的進程
ProcessStartInfo startInfo = new ProcessStartInfo();//啟動進程時使用的集合
//startInfo.FileName = Environment.CurrentDirectory + "\\Release1\\a.exe";//要啟動的應用程序
startInfo.FileName = "E:\\work\\Release\\a.exe";//要啟動的應用程序
startInfo.WindowStyle = ProcessWindowStyle.Normal;//啟動應用程序時使用的窗口狀態
//startInfo.WorkingDirectory = Environment.CurrentDirectory + "\\Release1\\";//要啟動應用程序的路徑
newProcess.StartInfo = startInfo;//把啟動進程的信息賦值給新建的進程
newProcess.StartInfo.UseShellExecute = false;//是否使用操作系統shell執行該程序
newProcess.Start();
}
catch
{
//退出控制台程序
Application.Exit();
}