how to close another app in system with c# code example

Example 1: how to close another app in system with c#

Process[] runningProcesses = Process.GetProcesses();
foreach (Process process in runningProcesses)
{
    // now check the modules of the process
    foreach (ProcessModule module in process.Modules)
    {
        if (module.FileName.Equals("MyProcess.exe"))
        {
            process.Kill();
        } else 
        {
         enter code here if process not found
        }
    }
}

Example 2: how to close another app in system with c#

using System.Diagnostics;

public void CloseProccessOfAnotherApplication(string name)
        {
            
                if (string.IsNullOrWhiteSpace(name))
                {
                    iap.WriteLog("Process Name Was Null! Error. Please Pass A Process Name");
                    Environment.Exit(1);
                }
                Process[] mainAppProcessOrProcessesRunning = Process.GetProcessesByName(name);
                foreach(var p in mainAppProcessOrProcessesRunning)
                 {
                p.Kill();
                // or p.close(); 
                /*If you have permission issues then you have to capture each MainWindow using the proccess, focus that window, and do mainWindow.Exit(). 
                This may require using system32 automation to mimic the user. You may also have to pull each Modal attached to the main window and close those before hand.*/
              
                 }
            
        }

Example 3: how to close another app in system with c#

using System.Diagnostics;

public void CloseProccessOfAnotherApplication(string name)
        {
            
                if (string.IsNullOrWhiteSpace(name))
                {
                    iap.WriteLog("Process Name Was Null! Error. Please Pass A Process Name");
                    Environment.Exit(1);
                }
                Process[] mainAppProcessOrProcessesRunning = Process.GetProcessesByName(name);
                foreach(var p in mainAppProcessOrProcessesRunning)
                 {
                p.Kill();
                // or p.close(); 
                /*If you have permission issues then you have to capture each MainWindow using the proccess, focus that window, and do mainWindow.Exit(). 
                This may require using system32 automation to mimic the user. You may also have to pull each Modal attached to the main window and close those before hand.*/
              
                 }
            
        }

Tags:

Misc Example