[Implement] 取得執行中應用程式的 Process ID 並關閉應用程式
{
int result = -1; //-1 = not found, -2 = error
System.Diagnostics.Process[] localByName =
System.Diagnostics.Process.GetProcessesByName(processName);
int i = localByName.Length;
System.Diagnostics.Process chosen;
try
{
while (i > 0)
{
chosen = System.Diagnostics.Process.GetProcessById(localByName[i - 1].Id);
if (chosen.ProcessName == processName &&
chosen.MainWindowTitle.Contains(mainWindowsTitle))
{
result = localByName[i - 1].Id;
}
i -= 1;
}
}
catch (Exception ex)
{
result = -2;
}
return result;
}
public string KillProcess(int processID)
{
string result = "success";
System.Diagnostics.Process chosen;
try
{
chosen = System.Diagnostics.Process.GetProcessById(processID);
chosen.Kill();
chosen.WaitForExit();
}
catch (Exception ex)
{
result = "fail, " + ex.Message;
}
return result;
}