简体   繁体   中英

Unable to kill a process in C#?

I'm currently developing a program that can inject Lua Scripts into Roblox (yes I know), everything is working fine so far, except the kill process. As Roblox works as a single process, I don't need to kill the tree. I have done a bit of research regarding the topic and none of them work.

Roblox runs as a process called RobloxPlayer.exe, I believe that the Process.GetProcessesByName should fetch the process via the name of it. Being Roblox, not RobloxPlayer.exe

Process[] processes = Process.GetProcessesByName("Roblox");
foreach (Process process in processes)
{
    process.Kill();
    process.WaitForExit();
}

You could do string matching of all processes and terminate any process that matches your criteria.

String findThisProcess = "Roblox";

foreach (Process process in Process.GetProcesses()) //provides you with a list of all running processes
{
    try
    {           
        if (process.ProcessName.Contains(findThisProcess, StringComparison.InvariantCultureIgnoreCase))
        {
            Console.WriteLine($"Process: {Path.GetFileName(process.MainModule.FileName)}, Name: {process.ProcessName}");
            process.Kill();
            process.WaitForExit();
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("Couldn't terminate process: " + ex);
    }
}

Edit: Added InvariantCultureIgnoreCase so that it matches processes, regardless of their case. Eg. VsDebugConsole.exe will be matched even if you input vsdebug .

Get the name of current process in a variable.

var appName = Process.GetCurrentProcess();
var isInstanceAlready = Process.GetProcessesByName(appName.ProcessName).Where(p => p.Id != appName.Id).FirstOrDefault();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM