tags:

views:

395

answers:

2

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

A: 

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.

Argalatyr
+2  A: 

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
}
ChrisV
by using the above code, shutdown window appears. i would like to close without shutdown system
JKS
So add a bit more logic to the if statement. Make sure you're not trying to take down WinLogon.exe and similar processes. Consider P/Invoking to EnumWindows() and sending the WM_CLOSE call manually.
ChrisV