Suriyan Suresh
Suriyan Suresh

Reputation: 3024

how to close all running applications safely with c#

how to close all running applications safely with c# without using windows logoff & shutdown API function. After closing all application i would like to show my application

Upvotes: 3

Views: 9427

Answers (3)

Sarah Weinberger
Sarah Weinberger

Reputation: 15561

I realize that this question is ancient, about 10-years old, however I wanted to expand a bit on ChrisV's code and give the code necessary to shutdown other applications without shutting down Windows. Checking "winlogon", not "winlogon.exe", does not work.

using System.Diagnostics.

Process me = Process.GetCurrentProcess();
foreach (Process p in Process.GetProcesses())
{
    if (p.Id != me.Id && IntPtr.Zero != p.MainWindowHandle && false == p.ProcessName.Equals("explorer", StringComparison.CurrentCultureIgnoreCase))
    {
        // Sends WM_CLOSE; less gentle methods available too.
        p.CloseMainWindow();
    }
}

Additionally, run the above code with administrative privileges, so admin applications close too.

Note: File Explorer windows will stay open, as the Windows desktop is explorer.

I tested on Windows 10 and works great.

Process.StartInfo.FileName on Windows 10 returns empty strings, so targeting file names will not work.

Note: p.MainWindowHandle is zero if there is no main window, hence an application, but there are many cases which are not applications and still have a MainWindowHandle. I should know. I created an applet that displays non-zero MainWindowHandle and I saw many of them, but only had a couple of applications open, when I tested. Still, this code does prevent Windows from asking to shut down.

Upvotes: 1

ChrisV
ChrisV

Reputation: 3423

If this is intended to be a replacement for the standard Windows login mechanism, you're not doing it right. On 2000/XP, consider writing a custom GINA replacement. On Vista/7, you need to write a new credential provider, as they got rid of the old GINA/Winlogon stack.

If you're trying to do a custom service of some sort instead, have you considered simply switching desktops? It's basically what Windows does for both Ctrl+Alt+Delete and the UAC "Secure Desktop." Far less work than killing the entire system and repopulating.

Seriously, reflect carefully on what you're about to do. The following is almost certainly the wrong thing, and may not always work with some programs:

// I mean it! This will cause badness!
using System.Diagnostics;

Process me = Process.GetCurrentProcess();
foreach (Process p in Process.GetProcesses())
{
    if (P.Id != me.Id)
        P.CloseMainWindow(); // Sends WM_CLOSE; less gentle methods available too
}

Upvotes: 5

Argalatyr
Argalatyr

Reputation: 4659

You could iterate through the running processes, and then you need to decide how "gentle" you want to be in closing other processess. Note that sending a message, such as WM_CLOSE, will not force them to close.

Upvotes: 1

Related Questions