简体   繁体   中英

need to close all running applications without using windows logoff from c# application

i need to close all open applications in windows while my application closes how to i do that using c#

i am implementing login/logoff functionality in my application itself instead of windows login/logoff so that i need to close all applications while logoff clicked in my application and pass control to my application after that

using System.Diagnostics;

Process [] localAll = Process.GetProcesses();
foreach(Process p in localAll)
{
    p.Kill();
}

But you'd probably have to put in some kind of filter to avoid killing certain processes since otherwise the machine might get a bit upset.

For whatever reason you want to do this. This is a bit funnier way to do it!

using System.Diagnostics;

Parallel.ForEach(Process.GetProcesses(), process => process.Kill());

Don't run this with an elevated process though; will probably caus your OS to crash.

You definitely do NOT want to just loop over the system processes and kill them all. There are plenty of things running that should not be killed. If you 'log out' in windows then log in under another user, there are a lot of processes that stay running at the system level and are shared by all logins.

As a good example, if I look in Windows Task Manager at the processes that my current user owns (not system), then I have ATI Catalyst Control running, the Synaptics app for my touchpad, a Logitech app for my mouse, something fo the Bluetooth stack, something for the Audio system, a Dell wireless lan interface management thing... If I kill all those, I suspect my peripheral devices will start behaving strangely, even if another user logs in after me.


Could you just call the actual Windows LogOut stuff with P/Invoke, as suggested here ? That way Windows itself can do... well, whatever it does when a user logs out, instead of you having to deal with it yourself.

At worst , I would try to collect up the things that were started by Windows during startup. The 'Statup' items from the StartMenu:

C:\\Users\\User_Name\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup

and the items from the registry at:

HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run

Then loop over the running processes and see which really belong to this user and only stop those.

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