public int GetProcessID(string processName, string mainWindowsTitle)

{

    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;

}

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 Big Bear 的頭像
    Big Bear

    Programs Knowledge

    Big Bear 發表在 痞客邦 留言(0) 人氣()