启动外部进程:
public bool StartProc(string Path, string ProcName)
{
bool result = false;
System.Collections.ArrayList procList = new System.Collections.ArrayList();
string tempName = "";
Process[] temp = Process.GetProcessesByName(ProcName);//在所有已启动的进程中查找需要的进程;
if (temp.Length > 0)//如果查找到
{
IntPtr handle = temp[0].MainWindowHandle;
SwitchToThisWindow(handle, true); // 激活,显示在最前
}
else
{
Process.Start(Path + ProcName + ".exe");//否则启动进程
}
foreach (System.Diagnostics.Process thisProc in System.Diagnostics.Process.GetProcesses())
{
tempName = thisProc.ProcessName;
procList.Add(tempName);
if (tempName == ProcName)
{
result = true;
}
}
return result;
}
[DllImport("user32.dll")]
public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
public bool closeProc(string ProcName)
{
bool result = false;
System.Collections.ArrayList procList = new System.Collections.ArrayList();
string tempName = "";
foreach (System.Diagnostics.Process thisProc in System.Diagnostics.Process.GetProcesses())
{
tempName = thisProc.ProcessName;
procList.Add(tempName);
if (tempName == ProcName)
{
if (!File.Exists(PublicValue.ExePath + "\\" + ProcName + ".bat"))
{
FileInfo myfile = new FileInfo(PublicValue.ExePath + "\\" + ProcName + ".bat");
FileStream fs = myfile.Create();
fs.Close();
StreamWriter sw = File.AppendText(PublicValue.ExePath + "\\" + ProcName + ".bat");
sw.WriteLine("@echo off");
sw.WriteLine("taskkill /f /t /im " + ProcName + ".exe\n");
sw.WriteLine("echo.&echo.");
sw.WriteLine("del " + PublicValue.ExePath + "\\" + ProcName + ".bat");
sw.Flush();
sw.Close();
}
Process.Start(PublicValue.ExePath + "\\" + ProcName + ".bat");
result = true;
}
}
return result;
}
PublicValue.ExePath = System.Windows.Forms.Application.StartupPath;