亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用Vb.NET或C#終止進程?

如何使用Vb.NET或C#終止進程?

C#
慕村9548890 2019-12-27 15:11:34
我有一種情況,我必須檢查用戶是否已經打開了Microsoft Word。如果他有,那么我必須終止winword.exe進程并繼續執行我的代碼。是否有任何簡單的代碼可以使用vb.net或c#殺死進程?
查看完整描述

3 回答

?
胡子哥哥

TA貢獻1825條經驗 獲得超6個贊

您將要使用System.Diagnostics.Process.Kill方法。您可以使用System.Diagnostics.Proccess.GetProcessesByName獲得所需的進程 。


示例已經在此處發布,但是我發現non.exe版本的效果更好,所以類似:


foreach ( Process p in System.Diagnostics.Process.GetProcessesByName("winword") )

{

    try

    {

        p.Kill();

        p.WaitForExit(); // possibly with a timeout

    }

    catch ( Win32Exception winException )

    {

        // process was terminating or can't be terminated - deal with it

    }

    catch ( InvalidOperationException invalidException )

    {

        // process has already exited - might be able to let this one go

     }

}

您可能不必處理NotSupportedException,這表明該過程是遠程的。


查看完整回答
反對 回復 2019-12-27
?
Smart貓小萌

TA貢獻1911條經驗 獲得超7個贊

 public bool FindAndKillProcess(string name)

    {

        //here we're going to get a list of all running processes on

        //the computer

        foreach (Process clsProcess in Process.GetProcesses()) {

            //now we're going to see if any of the running processes

            //match the currently running processes by using the StartsWith Method,

            //this prevents us from incluing the .EXE for the process we're looking for.

            //. Be sure to not

            //add the .exe to the name you provide, i.e: NOTEPAD,

            //not NOTEPAD.EXE or false is always returned even if

            //notepad is running

            if (clsProcess.ProcessName.StartsWith(name))

            {

                //since we found the proccess we now need to use the

                //Kill Method to kill the process. Remember, if you have

                //the process running more than once, say IE open 4

                //times the loop thr way it is now will close all 4,

                //if you want it to just close the first one it finds

                //then add a return; after the Kill

                try 

                {

                    clsProcess.Kill();

                }

                catch

                {

                    return false;

                }

                //process killed, return true

                return true;

            }

        }

        //process not found, return false

        return false;

    }


查看完整回答
反對 回復 2019-12-27
  • 3 回答
  • 0 關注
  • 683 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號